OnRestart vs. OnResume - Android Lifecycle Question

坚强是说给别人听的谎言 提交于 2019-11-28 21:29:23
Peter-W

That would be because when unless your are using Fragments each "screen" in your application is a new activity, when you click the back button it restarts the activity of the page before it.

If I am understanding what you want to do correctly you want to put your code on onCreate, not onRestart.

SEE COMMENT THREAD FOR ANSWER

My observation is that its hard to tie the lifecycle events to user behavior on the device or emulator. Where your app is paused, if the device needs memory or wants to recover resources, it will terminate the activity, causing onCreate to be called. There is just too many scenarios to build an adequate state machine to tell yourself "how" or "why" your activity was terminated.

The only way I've found to manage this is to create a service to hold the application state and manually manage the state. The problem is trying to use the Activity state to manage the application state. The Activity design seems to have limitations that just make it a poor choice for achieving the goal you've stated.

Here is how to do this:-

  1. Have a base activity that all your activities are derived from.

  2. Add in to the base activity:-

    int nAppState;
    
    protected override void OnCreate(Bundle bundle)
    {
            base.OnCreate(bundle);
            nAppState = 0;
            .
            .
    }
    protected override void OnStop()
    {
            AppState();
            base.OnStop();
    }
    public static int IMPORTANCE_BACKGROUND = 400;
    
    protected override void AppState()
    {
            ActivityManager am = (ActivityManager)GetSystemService(Context.ActivityService);
            IList<ActivityManager.RunningAppProcessInfo> list2 = am.RunningAppProcesses;
            foreach (ActivityManager.RunningAppProcessInfo ti in list2)
            {
                    if (ti.ProcessName.ToLower() == "com.mycompany.myapp")
                    {
                            nAppState = ti.Importance;
                            break;
                    }
            }
    }
    protected override void OnRestart()
    {
            base.OnRestart();
            if (nAppState == IMPORTANCE_BACKGROUND)
            {
                    // Show a log in screen
                    RunOnUiThread(delegate { StartActivity(new Intent(this, typeof(LoginAppearActivity))); });
                    nAppState = 0;
            }
    }
    
  3. Please note that this is in Mono C#, it will be the same code for Java, I'll leave it up to you to convert it!!

Yes, your assertions for red and blue are correct.

However, note the alternate pathway from onPause() and onStop(). Process being killed for memory reasons is a) out of your control and b) imperceptible to you if you only use onRestart() to detect "coming back" to the activity.

You have an option to avoid the previous activity by avoiding/removing the activity to come in Stack by setting some flag before calling the startActivity(intent):

intent.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);

This will avoid the present activity to get called on back press. Alternatively you can also ovverride the onBackPressed() method of the current activity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!