简体   繁体   English

按下主页按钮并返回应用程序后如何恢复以前的活动

[英]How to restore previous activity after pressed home button and return to the app

the flow is like below.流程如下。

  • MainActivity -> Second Activity MainActivity -> 第二个活动
  • Press home key then device main screen shown.按主页键然后显示设备主屏幕。
  • Enter to app box and click the application icon.进入应用程序框并单击应用程序图标。

I expect the second activity should be launched but main activity launched.我希望应该启动第二个活动,但启动主要活动。

is there any way that I control it -?!有什么办法让我控制它——?!

The default Android behaviour when you press the home button is that the current activity is pushed onto the stack, unless you called finish() in your activity.当您按下主页按钮时,Android 的默认行为是当前活动被推入堆栈,除非您在活动中调用了 finish()。 Whenever you open your application it will launch the activity that's on top of your stack.每当您打开应用程序时,它都会启动位于堆栈顶部的活动。

I'ts based on your system memory.我不是基于你的系统内存。 If you don't have enough memory to keep your activity backgrounded then Android will kill your activity and its state to recoup some memory.如果您没有足够的内存来保持您的活动后台运行,那么 Android 将终止您的活动及其状态以收回一些内存。 I'm thinking that perhaps your phone (or emulated device) is running short of memory.我在想,也许您的手机(或模拟设备)内存不足。

Check if you are using for that activity, any android:noHistory="true" within your App's manifest.检查您是否正在使用该活动,应用清单中的任何android:noHistory="true" Try removing it.尝试删除它。

You filtered your main activity by您过滤了您的主要活动

<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

so it should get launched when you click on the icon in your launcher.因此,当您单击启动器中的图标时,它应该会启动。 I think you should handle your pages with fragments to have the behavior you want.我认为你应该用片段处理你的页面以获得你想要的行为。

boolean isSentToBackground = false;

public void onResume(){
 if(isSentToBackground) {
  finish();
 }
}

public void onPause(){
 isSentToBackground = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM