App always starts fresh from root activity instead of resuming background state (Known Bug)

前端 未结 5 1879
庸人自扰
庸人自扰 2020-11-28 22:00

I am facing exactly the problem mentioned in these links:

http://code.google.com/p/android/issues/detail?id=2373

http://groups.google.com/group/android-devel

相关标签:
5条回答
  • 2020-11-28 22:21

    Just add this in onCreate method of your launcher activity like this:

          @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if (!isTaskRoot()) {
            finish();
            return;
        }
        // other function
        }
    
    0 讨论(0)
  • 2020-11-28 22:29

    Here is the solution:

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
    
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0 & getIntent().getExtras() == null) {
            finish();
            return;
        }
    
     Your code....
    }
    

    EDIT: I had problems with new intents and notifications. The previous solution doesn't work with notifications and intents...

    0 讨论(0)
  • 2020-11-28 22:31

    Similar solution for Xamarin.Android:

    if (!IsTaskRoot)
                {
                    string action = this.Intent.Action;
                    if (this.Intent.HasCategory(Intent.CategoryLauncher) && !string.IsNullOrEmpty(this.Intent.Action) && action == Intent.ActionMain)
                    {
                        Finish();
                        return;
                    }
                }
    
    0 讨论(0)
  • 2020-11-28 22:40
        @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
            // Activity was brought to front and not created, 
            // Thus finishing this will get us to the last viewed activity 
            finish(); 
            return; 
        } 
    
        // Regular activity creation code... 
    } 
    
    0 讨论(0)
  • 2020-11-28 22:41

    This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.

    Whoever submitted the bug should have probably worded it as a request for enhancement to the Eclipse plugin since they apparently want Eclipse to have the ability to pretend to be the launcher and to start apps using the same intent as the launcher.

    0 讨论(0)
提交回复
热议问题