How to reuse Activities? Not to create activity each time

后端 未结 2 1678
时光取名叫无心
时光取名叫无心 2021-02-20 14:28

This topic is continue of this: Android. How to start activity without creating new one?

I have read that activities are destroyed when to click BACK button. They can be

相关标签:
2条回答
  • 2021-02-20 14:55

    You can redefine the "onBack" method of your activity, something like

    public void onBackPressed () 
    {
        moveTaskToBack (true);
    }
    
    0 讨论(0)
  • 2021-02-20 15:06

    The default implementation of the back button is the finish the current activity. You may however intercept that key press and do whatever you wish with it. For instance, instead of finishing your current activity, you could "bring up" the previous activity and thus making it seem as if the normal implementation is at hand.

    To intercept the back button press: Android: intercepting back key

    And to start your previous activity without creating a new one every time:

    Intent i = new Intent(this, PreviousActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(i);
    

    In Kotlin 1.2:

    val intent = Intent(this, RepairListActivity::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
    startActivity(intent)
    

    Good luck.

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