【问题标题】:finish() called in FragmentActivity with onDestroyView in fragment kills previous activity在 FragmentActivity 中使用 onDestroyView 调用的 finish() 会杀死先前的活动
【发布时间】:2015-05-06 08:23:12
【问题描述】:

设置:

我有一个带有两个片段的活动,这些片段动态放置在框架布局内。其中一个片段是具有以下 onDestroyView 实现的地图片段。

@Override
public void onDestroyView() {
    super.onDestroyView();
    try{
        MapFragment fragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
        FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }catch(Exception e){
        Log.e("SomeTag", "someError");
    }
}

此外,我的 Activity 实现了 onOptionsItemSelected 并在用户按下 ActionBar 中的主页按钮时调用 finish()。

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MyActivity"
    android:padding="0dp"
    android:layout_margin="0dp">
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>

在 Activity 中添加片段:

private void addFragmentToView(Fragment fragment){
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack so the user can navigate back
    transaction.replace(R.id.fragment_container, fragment);
    transaction.addToBackStack(null);
    // Commit the transaction
    transaction.commit();
}

Activity 的 onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
        case android.R.id.home:
            finish();
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

问题: 当我在地图片段中并单击 ActionBar 的后退按钮时,活动接收到单击并调用 finish()。然后地图片段的 onDestroyView 启动并尝试销毁该片段。但是,发生的情况是应用程序切换到此活动之前的活动,然后完全退出应用程序(因为我之前的活动是第一个活动)。

当我在另一个片段中并执行完全相同的操作时,看不到此行为,但该片段不会在 onDestroyView 中自行删除。

问题: 什么是处理这种情况并允许地图片段自行销毁但又返回到之前的活动的好方法?

【问题讨论】:

  • 您是否在 Activity 的 onDestroy 方法中调用 finish()?如果是这样,停止它
  • 不。我在主要活动内的 onOptionsItemSelected 内调用 finish() 。我在地图片段中使用 onDestroy 在离开片段之前将其删除,因为当我尝试再次加载它时,如果尚未完全删除它会导致错误。

标签: android android-activity android-fragments


【解决方案1】:

发生这种情况是因为您没有将片段添加到后台堆栈 使用 addToBackStack() 添加回栈

getSupportFragmentManager().beginTransaction()
                           .add(detailFragment, "detail")
                           // Add this transaction to the back stack
                           .addToBackStack()
                           .commit();

http://developer.android.com/training/implementing-navigation/temporal.html

【讨论】:

    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多