简体   繁体   English

每当应用程序来自android中的后台时,启动“第一屏幕”

[英]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: 在这里看看:

Managing Tasks 管理任务
clearTaskOnLaunch clearTaskOnLaunch

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

相关问题 在Android中,在第一次启动手机应用程序时,活动前白屏显示 - In android getting white screen before activity when first time app start on phone 如何在用户下次打开应用程序/进入 Android 的主屏幕时删除正确回答的问题/图像? - How to remove a question/image that was answered correctly the next time user opens the app/comes to the home screen in Android? 从 android 的后台启动活动或全屏意图 - Start activity or full screen intent from background in android 当应用程序来自后台时询问PIN码 - Ask PIN when app comes from Background 全屏启动器应用程序来自后台后,Android NFC读取失败 - Android NFC read fails after fullscreen launcher app comes from background Android studio-每次按下按钮时从头开始发出声音 - Android studio - Start a sound from the beginning each time a button is pressed 第一次应用启动时 Android ResourcesNotFoundException - Android ResourcesNotFoundException on first App start Android:如何仅在首次启动应用程序时才将相机移到我的位置? - Android: How to move camera to my location only on each time I first start my application? 当应用程序从后台进入前台时,onBackPress() 不起作用 - onBackPress() is not working when app comes to foreground from background 当应用程序来自后台时更改菜单图标(在onResume()方法中) - Change Menu icons when app comes from background (in onResume() method)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM