简体   繁体   中英

Always launch the main activity

My application has 4 activities. If the user clicks the home button, the next time the application is launched, I want it to start on the main activity not the last activity it was on. If the phone's display turns off, as long as the application hasn't been closed, I want it to resume where it left off. What is the proper way to go about this?

Just set clearTaskOnLaunch="true" in your manifest main activity declaration.

http://developer.android.com/guide/topics/manifest/activity-element.html#clear

This doesn't answer all parts of your question, but it sounds as though you'd benifit from looking at these attributes in the manifest; launchmode , finishOnTaskLaunch , clearTaskOnLaunch .

I suppose it is a problem of affinity and recent list. Here is how i solved it.

android:taskAffinity="com.packageName.Excluded" android:excludeFromRecents="true"

so all tasks with the same affinity will not be listed in recent list. Note : excludeFromRecents will exclude all the tasks with same affinity in one go, so if you don't set the affinity of the activity, your application will be excluded from recent list(app activities use same affinity by default if not set)

@Override
protected void onResume(){
    super.onResume();
    keeper += 1;
    if (keeper == 2){
        Intent iii = new Intent(MainActivity.this, SplashActivity.class);
        startActivity(iii);
        finish();
    }
}

You should implement all theses cases on the onPause and onResume methods of your Activity.

You should learn the Activity lifecycle http://developer.android.com/reference/android/app/Activity.html

In the onResume method, specify which activity to load.

This page from the Android documentation should shed some more light on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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