简体   繁体   中英

Is there a way to restart completely the app when Android kills the activity and the user starts it again?

My app has multiple activities, some of them with complex view models and a high dependency between them. When Android needs it, it kills the process and when the user navigates again to the app, OnCreate of the last activity used is called. To rebuild the former status is complex and with no value for the kind of the app. It would be a lot easier and robust that when the user navigates to the killed activity, simply the system relaunchs the app from scratch.

Is there a way to do it?

you could do like this

custon MyApplication.cs :

[Application]
public class MyApplication : Application
{

    public static int APP_STATUS_KILLED = 0; // Represents that the application was started after it was killed
    public static int APP_STATUS_NORMAL = 1; // Represents the normal startup process at application time
    public static int APP_STATUS = APP_STATUS_KILLED; // Record the startup status of the App
    private static Context context;

    public override void OnCreate()
    {
        base.OnCreate();

        context = GetAppContext();
    }

    public static Context GetAppContext()
    {
        return context;
    }

    /**
     * Reinitialize the application interface, empty the current Activity stack, and launch the welcome page
     */
    public static void ReInitApp()
    {
        Intent intent = new Intent(GetAppContext(), typeof(SplashActivity));
        intent.SetFlags(ActivityFlags.ClearTask| ActivityFlags.NewTask);
        GetAppContext().StartActivity(intent);
    }

}

create a SplashActivity.cs which is lanch activity:

[Activity(Label = "SplashActivity")]
public class SplashActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        MyApplication.APP_STATUS = MyApplication.APP_STATUS_NORMAL; //App starts normally. Set the startup status of App to normal startup
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.activity_splash);

        GoMain();

    }
    private void GoMain()
    {
        Intent intent = new Intent(this, typeof(MainActivity));
        StartActivity(intent);
        Finish();
    }
}

then create BaseActivity.cs ,all activities extends it beside SplashActivity:

[Activity(Label = "BaseActivity")]
public abstract class BaseActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        if (MyApplication.APP_STATUS != MyApplication.APP_STATUS_NORMAL)
        { // Start the process abnormally and reinitialize the application interface directly
            MyApplication.ReInitApp();
            Finish();
            return;
        }
        else
        { // Normal startup process
            SetUpViewAndData(savedInstanceState); // child Activity initialization
        }

    }
    //Provide an interface to the subactivity setup interface. Do not initialize in onCreate
    protected abstract void SetUpViewAndData(Bundle savedInstanceState);
}

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