androidandroid-activityactivitygroupactivity-stack

Activity stack or task for my application


I am using ActivityGroup in my application, structure is as follow A > B > C (where A display first), and B and C makes a loop, like B > C > B > C > B and at both B and C if a user press back button, activity A should be display(even if user is on C), and there no need of B and C in stack.

So how can I implement onBackPressed() or any other method to make my application.


Solution

  • If I would go into such situation then here is what I opt

    1. A activity is created and A will either call A or B
    2. Then B and C cycle in a manner when ever I create C from B or vice versa the calling activity must finish itself
    3. Finally On any activity whether B or C when back key is pressed it will destroy itself

    for over riding back key in B and C activity here is code

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }