我正在实现一个播放视频的应用程序。有2个活动:MainMenuActivity
(在纵向模式下)和VideoPlayerActivity
(可以旋转)。 MainMenuActivity具有HomeFragmentLT
片段。在HomeFragmentLT
片段中有一个ViewPager。这是我的HomeFragmentLT
和ViewPager(HomePager
):
public class HomeFragmentLT extends Fragment implements Observer
{
public static String TAG = "HomeFragmentLT";
private ViewGroup mLayout;
private View mHeader;
private HomeAdapter mHomeAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (mLayout == null)
mLayout = (ViewGroup) inflater.inflate(R.layout.home_listview, null);
else
((ViewGroup) mLayout.getParent()).removeView(mLayout);
initViews();
return mLayout;
}
public HomeFragmentLT(){}
private void initViews()
{
mPager = (ViewPager) mHeader.findViewById(R.id.home_pager);
.......
.......
}
@Override
public void onStart()
{
super.onStart();
if (!mDataReceived)
getData();
Log.e(TAG, "onStart");
}
@Override
public void onStop()
{
DataHelper.getInstance().deleteObserver(this);
super.onStop();
Log.e(TAG, "onStop");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e(TAG, "onDestroy");
}
private void getData()
{
if (mError)
{
UIUtils.toggleError(mLayout, false, null);
mListView.setOnTouchListener(null);
UIUtils.toggleLoading(mLayout, true);
}
DataHelper.getInstance().addObserver(this);
DataHelper.getInstance().getHomeData();
}
private void setData()
{
mHomeAdapter = new HomeAdapter(getActivity(), mPager, mHomeData.getHotPrograms());
mPager.setAdapter(mHomeAdapter);
mIndicator.setViewPager(mPager);
.............
}
@Override
public void update(Observable observable, Object data)
{
if (data == null)
return;
Result result = (Result) data;
if (Method.GET_HOME_DATA.equals(result.getParams().getMethod()))
{
if (result.getError() == null)
{
mHomeData = (HomeObj) result.getData();
setData();
mDataReceived = true;
mError = false;
DataHelper.getInstance().deleteObserver(this);
}
}
}
......................
public static class HomeAdapter extends PagerAdapter
{
private List<Program> mPrograms;
private View[] mViews;
private Context mContext;
public HomeAdapter(Context context, ViewPager viewpager, List<Program> programs){...........}
@Override
public int getCount()
{
return mPrograms.size();
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
View view;
if ((view = mViews[position]) != null)
{
ImageView img = (ImageView) view.findViewById(R.id.img);
if (img.getDrawable() == null)
{
UIUtils.toggleLoading(view, true);
ImageManager.getInstance().push(mPrograms.get(position).getThumbnail(), img, mLoadingListener);
}
return view;
}
view = LayoutInflater.from(mContext).inflate(R.layout.home_program_item_top, null);
view.setTag(mPrograms.get(position).getId());
view.setLayoutParams(mParams);
if (Constants.MAY_CUI_DETECTED)
ImageManager.getInstance().push(mPrograms.get(position).getThumbnail(),
(ImageView) view.findViewById(R.id.img), mParams.width, mParams.height, mLoadingListener);
else
ImageManager.getInstance().push(mPrograms.get(position).getThumbnail(),
(ImageView) view.findViewById(R.id.img), mLoadingListener);
view.setOnClickListener(mOnItemClick);
container.addView(view);
return mViews[position] = view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object){}
}
}
在
MainMenuActivity
中,我在HomeFragmentLT
中添加onCreate()
getSupportFragmentManager().beginTransaction().add(R.id.dummy, mHomeFragment).commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
一切正常。但是,当我将手机置于横向时,然后启动
VideoPlayerActivity
(即横向模式下的VideoPlayerActivity)以播放来自MainMenuActivity
(肖像)的视频,然后立即按“返回”按钮。有例外。这是我的日志:12-09 18:30:09.102: E/MainMenuActivity(23287): onCreate()
12-09 18:30:09.332: E/MainMenuActivity(23287): onStart()
12-09 18:30:09.432: E/MainMenuActivity(23287): onResume()
12-09 18:30:09.703: E/HomeFragmentLT(23287): onCreate()
12-09 18:30:09.703: E/HomeFragmentLT(23287): onCreateView()
...........
12-09 18:30:14.487: E/MainMenuActivity(23287): onPause()
12-09 18:30:14.788: E/VideoPlayerActivity(23287): onCreate()
12-09 18:30:14.788: E/VideoPlayerActivity(23287): onStart()
...........
12-09 18:30:15.418: E/VideoPlayerActivity(23287): onPause()
12-09 18:30:15.588: E/HomeFragmentLT(23287): onStop()
12-09 18:30:15.639: E/MainMenuActivity(23287): onDestroy()
12-09 18:30:15.659: E/MainMenuActivity(23287): onCreate()
12-09 18:30:15.659: E/HomeFragmentLT(23287): onCreate()
12-09 18:30:15.719: E/MainMenuActivity(23287): onStart()
12-09 18:30:15.789: E/HomeFragmentLT(23287): onCreateView()
12-09 18:30:15.979: E/MainMenuActivity(23287): onResume()
12-09 18:30:16.169: E/MainMenuActivity(23287): onPause()
12-09 18:30:16.169: E/HomeFragmentLT(23287): onStop()
12-09 18:30:16.179: E/MainMenuActivity(23287): onDestroy()
12-09 18:30:16.199: E/MainMenuActivity(23287): onCreate()
12-09 18:30:16.199: E/HomeFragmentLT(23287): onCreate()
12-09 18:30:16.239: E/MainMenuActivity(23287): onStart()
12-09 18:30:16.259: E/HomeFragmentLT(23287): onCreateView()
12-09 18:30:16.379: E/MainMenuActivity(23287): onResume()
12-09 18:30:16.469: E/AndroidRuntime(23287): FATAL EXCEPTION: main
12-09 18:30:16.469: E/AndroidRuntime(23287): java.lang.IllegalStateException: Activity has been destroyed
12-09 18:30:16.469: E/AndroidRuntime(23287): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358)
12-09 18:30:16.469: E/AndroidRuntime(23287): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
12-09 18:30:16.469: E/AndroidRuntime(23287): at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
12-09 18:30:16.469: E/AndroidRuntime(23287): at com.vng.zingtv.activity.MainMenuActivity$3.run(MainMenuActivity.java:375)
12-09 18:30:16.469: E/AndroidRuntime(23287): at android.os.Handler.handleCallback(Handler.java:615)
12-09 18:30:16.469: E/AndroidRuntime(23287): at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 18:30:16.469: E/AndroidRuntime(23287): at android.os.Looper.loop(Looper.java:137)
12-09 18:30:16.469: E/AndroidRuntime(23287): at android.app.ActivityThread.main(ActivityThread.java:4744)
12-09 18:30:16.469: E/AndroidRuntime(23287): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 18:30:16.469: E/AndroidRuntime(23287): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 18:30:16.469: E/AndroidRuntime(23287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-09 18:30:16.469: E/AndroidRuntime(23287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-09 18:30:16.469: E/AndroidRuntime(23287): at dalvik.system.NativeStart.main(Native Method)
有2个问题需要您的帮助。
1.为什么
MainMenuActivity
被销毁并重新创建? (我的LogCat)2.我的
HomeFragmentLT
片段中导致IllegalStateException
的问题是什么?提前致谢。
编辑
我希望我的VideoPlayerActivity根据设备方向进行旋转。因此在manifiedt.xml中
<activity
android:name=".activity.VideoPlayerActivity"
android:screenOrientation="portrait" />
并在VideoPlayerActivity的onCreate()中:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
initViews();
......
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
最佳答案
旋转设备意味着系统必须加载新资源。例如。您可能需要在某些layout-land
文件夹中添加不同的资源。为了能够加载这些新资源,活动被销毁,并使用新的drawables / layouts / values等重新创建。Android有一种用于此类更改的特殊机制,称为configChanges
。这包括设备旋转,还包括更改电话语言等。
为了避免破坏您的活动,您可以在AndroidManifest.xml
中声明,可以通过设置以下标志来自行处理方向更改:
<activity
android:name=".VideoPlayerActivity"
android:configChanges="orientation|screenSize" />
在您的活动中,添加以下代码:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
有关更多信息,请参见this link to the developer documentation