How to use IntentCompat.makeRestartActivityTask()?

前端 未结 3 561
抹茶落季
抹茶落季 2021-01-04 10:58

I\'m trying to implement a button that will result in my app going back to the first activity and acting as if it was (almost) restarted all over. This code

         


        
相关标签:
3条回答
  • 2021-01-04 11:40

    This is how I'm using IntentCompat

        Intent intentToBeNewRoot = new Intent(this, MainActivity.class);
        ComponentName cn = intentToBeNewRoot.getComponent();
    
        Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
    
        startActivity(mainIntent);
    

    This effectively replaces my no-longer-wanted task root with MainActivity. It works in Gingerbeard and ICS. I haven't seen the "is an undefined symbol" message.

    0 讨论(0)
  • 2021-01-04 11:50

    Update

    Google has removed the method IntentCompat.makeRestartActivityTask() in current support library versions. Instead, you can just use the plain Android API:

    ComponentName cn = intent.getComponent();
    Intent.makeRestartActivityTask(cn);
    

    I hope this can save someone time searching for alternatives ;)

    0 讨论(0)
  • 2021-01-04 11:54

    IntentCompat is deprecated which is may be deleted, but Intent class this static method

    Intent mainIntent = Intent.makeRestartActivityTask(cn);

    So just use the above statement.

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