Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在FragmentActivity中调用finish(),并在fragment中使用onDestroyView终止以前的活动_Android_Android Activity_Android Fragments - Fatal编程技术网

Android 在FragmentActivity中调用finish(),并在fragment中使用onDestroyView终止以前的活动

Android 在FragmentActivity中调用finish(),并在fragment中使用onDestroyView终止以前的活动,android,android-activity,android-fragments,Android,Android Activity,Android Fragments,设置: 我有一个活动,它包含两个动态放置在framelayout中的片段。其中一个片段是具有以下onDestroyView实现的映射片段 @Override public void onDestroyView() { super.onDestroyView(); try{ MapFragment fragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map); Frag

设置:

我有一个活动,它包含两个动态放置在framelayout中的片段。其中一个片段是具有以下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");
    }
}
此外,“我的活动”实现OnOptions ItemSelected,并在用户按下ActionBar中的home按钮时调用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>
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();
}
@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);
    }
}
活动的OnOptions项已选定:

<?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>
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();
}
@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);
    }
}
问题: 当我在map片段中,单击ActionBar的back按钮时,活动将收到单击并调用finish()。然后,地图片段的onDestroyView启动并尝试销毁该片段。但是,发生的情况是,应用程序切换到此活动之前的活动,然后完全退出应用程序(因为我以前的活动是第一个活动)

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

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

这是因为您没有将片段添加到后堆栈中 使用addToBackStack()添加到后堆栈中

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

您是否在活动的
onDestroy
方法中调用
finish()
?如果是这样的话,停下来不。我在主活动中的OnOptions ItemSelected内调用finish()。我在映射片段中使用onDestroy在离开片段之前删除它,因为当我再次尝试加载它时,如果它没有被完全删除,就会导致错误。