问题描述
首先,我有两个活动:启动和MainActivity(只支持纵向)。在MainActivity,我有很多的片段用幻灯片菜单。我想保持当前片段,当用户离开MainActivity。这里是我的尝试:
First, I have two activities: Splash and MainActivity ( only support portrait). In MainActivity, I have many fragments use Slide menu. I want to keep current fragment when user leave MainActivity. Here is my try:
int currentFragment = 0;
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
currentFragment = savedInstanceState.getInt(CURRENT_FRAGMENT_KEY, 0);
switchContent(currentFragment);
} else {
// change fragment by index
switchContent(0);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(CURRENT_FRAGMENT_KEY, currentFragment);
Log.d("onSaveInstanceState" ," current fragment" + currentFragment);
super.onSaveInstanceState(outState);
}
我的清单
:
<activity
android:name="com.appiphany.auskills.activity.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:screenOrientation="portrait" />
一切都很好,当我建立我的应用程序与调试的关键:preSS Home键,然后再返回到应用程序,它打开previous片段。但是,当我建立在释放模式(用我的私钥,我不使用 ProGuard的
)的MainActivity,preSS HOME键,然后打开应用程序再次,从SplashActivity 。我不知道这个奇怪的问题。我的事件试试这个,但它并不能帮助:
Everything is fine when I build my app with debug key: press Home button, then back to the app, it open previous fragment. But when I build on release mode (use my private key, I don't use proguard
), press HOME button in MainActivity, then open app again, it starts from SplashActivity. I have no idea with this strange issue. I event try this one but it doesn't help:
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currentFragment = savedInstanceState.getInt(CURRENT_FRAGMENT_KEY, 0);
switchContent(currentFragment);
}
有没有什么想法?
Is there any ideas?
更新:我发现了另一个奇怪的:当我从APK文件安装此问题只发生。安装后,手机会询问2个选项:完成
或打开
。如果我preSS 打开
,这个问题发生了。当我杀了通过任务管理器应用程序,然后重新打开,它正常工作。
Update: I found another strange one: this issues only occurred when I install it from apk file. After install, phone will ask 2 options: Done
or Open
. If I press open
, this issues happened. When I kill app by task manager, then open again, it work correctly.
推荐答案
问题已得到修复使用 FLAG_ACTIVITY_NEW_TASK
时,导航距离Splash主活动。我不知道为什么它可以在调试模式下工作(重现从APK像释放模式安装)。这是我的可行$ C $下任何人有同样的问题:
Issue has been fixed by using FLAG_ACTIVITY_NEW_TASK
when navigate from Splash to Main activity. I just wonder why it can work in debug mode (Reproduce install from apk like release mode). Here is my workable code for anyone has same issues:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
startActivity(intent);
更新:非常感谢大卫·瓦瑟
,所以正确答案应该是这样的:
Update: many thanks to David Wasser
, so the right answer should like this:
// SplashActivity
if(!isTaskRoot()) {
finish();
return;
}
参考:Re-launch活动的Home键,不过......只有第一次的
这篇关于活动仅在释放模式丧生preSS HOME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!