Android Activity.finish()不完成活动
我有一个活动有三个片段。我还重写了活动中的Android Activity.finish()不完成活动,android,android-fragments,Android,Android Fragments,我有一个活动有三个片段。我还重写了活动中的onBackPressed()方法。当用户导航到我的任何片段时,会出现后退按钮的标准行为。除非我不是从它的“父级”启动一个片段 想象一下:我的活动没有UI。它只有5个片段。当用户处于fragment1、fragment2或fragment3并按下后退按钮时,他必须退出应用程序(即,仅在应用程序的活动中调用finish()Fragment1Child在概念上是fragment1的“子级”,而fragment2Child是fragment2的“子级”。当从f
onBackPressed()
方法。当用户导航到我的任何片段时,会出现后退按钮的标准行为。除非我不是从它的“父级”启动一个片段
想象一下:我的活动没有UI。它只有5个片段。当用户处于fragment1
、fragment2
或fragment3
并按下后退按钮时,他必须退出应用程序(即,仅在应用程序的活动中调用finish()Fragment1Child
在概念上是fragment1
的“子级”,而fragment2Child
是fragment2
的“子级”。当从fragment1
或fragment2
到他们的孩子并按下后退键时,我得到了预期的行为。但是,我从fragment3
转到fragment2Child
,然后按back键,我将正确显示fragment2
。现在,当我按下后退键时,我使用的Activity.finalize()
方法被执行,但应用程序未关闭。相反,我回到片段3
下面是我的onBackPressed()方法,它检查当前显示的片段是什么,并决定相应的操作
@Override
public void onBackPressed() {
Log.d(TAG, "MainACtivity.onBackPressed()");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
Log.d("NIBHA", " Drawere ++++++++++++++++++++++");
mDrawerLayout.closeDrawers();
} else {
Fragment fragment = getSupportFragmentManager().findFragmentById(
R.id.fragment_container);
Log.d("Nibha", "" + fragment);
if (fragment instanceof DeliveryOrder
|| fragment instanceof IncomingShipment
|| fragment instanceof RentalAgreement) {
Log.d(TAG, "EXITING APP");
finish();
return;
}
if (fragment instanceof IncomingShipmentDetail
&& getIntent().getAction().equals("RA")) {
Log.d(TAG, "IS DETAIL CALLED FROM RA");
transaction.replace(R.id.fragment_container,
new IncomingShipment());
transaction.commit();
}
super.onBackPressed();
}
}
问题似乎是super.onBackPressed();没有人打电话。删除return语句并将以下内容更改为
,如果:
if (fragment instanceof IncomingShipmentDetail
&& getIntent().getAction().equals("RA")) {
Log.d(TAG, "IS DETAIL CALLED FROM RA");
transaction.replace(R.id.fragment_container,
new IncomingShipment());
transaction.commit();
}
是否尝试删除“super.onBackPressed();”?我试过了。当我必须完成()应用程序时,我仍然会被扔进碎片3。你能在logcat中看到“正在退出的应用程序”吗?这是奇怪的东西,是的,我看到了。。。