[英]Start the First screen each time app comes from background in android
I have an app that has 5 activities (A,B,C,D,E). 我有一个包含5个活动(A,B,C,D,E)的应用。 The app can navigate between those activities. 该应用程序可以在这些活动之间导航。 When the user presses home button in device the app goes background and after when the app comes to foreground, first activity should be launched ie A activity. 当用户按下设备中的主页按钮时,应用程序将变为后台,而当应用程序变为前台时,则应启动第一个活动,即活动。
Example: app in D activity, after pressing home, the app goes background and when it comes to foreground again it should open A activity not D. 范例:应用程式处于D活动,按下主画面后,应用程式会变成背景,当再次回到前台时,应开启A活动而非D。
Solutions which i have tried is launch mode, I set the launch mode for A activity (singleInstance) but could not able to find the required solution. 我尝试过的解决方案是启动模式,我为A活动(singleInstance)设置了启动模式,但找不到所需的解决方案。
For launch same Activty you Should clear all the Activity when app goes into background.When app goes background use below code that will clear current activity and all other activity that are in stack. 对于启动相同的活动,您应该在应用程序进入后台时清除所有活动。当应用程序进入后台时,请使用以下代码清除当前活动以及堆栈中的所有其他活动。
For API 16+, use 对于API 16+,请使用
finishAffinity(); finishAffinity();
For lower (Android 4.1 lower), use 对于较低版本(Android 4.1以下版本),请使用
ActivityCompat.finishAffinity(YourActivity.this); ActivityCompat.finishAffinity(YourActivity.this);
When you press Home-Button change to Activity A. Maybe this will work: 当您按Home-Button更改为Activity A时,可能会起作用:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_HOME)
{
Intent intent = new Intent(this, MyActivityName.class);
//replace MyActivityName.class with the name of your Activity A
startActivity(intent);
}
return super.onKeyDown(keyCode, event);
}
you may get ondestroy() or onpause() method. 您可能会使用ondestroy()或onpause()方法。 on it youcan do 你可以做
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); //
You can set android:clearTaskOnLaunch="true" for Activity A in your manifest file to achieve what you want: 您可以在清单文件中为活动A设置android:clearTaskOnLaunch =“ true”,以实现所需的功能:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.beispieldomain.stackoverflowxmlparse">
<application
...
<activity
android:name=".MainActivity"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Have a look here: 在这里看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.