Search code examples
androidandroid-activitychildactivity

How to BringtoFront the Parent Activity in Android?


As we know, startActivityForResult() used to get a result for a task from parent activity. Here when we click the setResult(). It returns the result to the parent Activity.

Child Activity means it should maintain the persistent state. That is when the users clicks a button on the child activity. It do not finishes that activity and show the Parent Activity. And again i may go to the child activity from parent activity. If the child does not exists it starts a new child activity else it should show the existing child activity with persistent content.

Edit:

Simply said, my parent Activity starts a new Child Activity. Now both Activities should be alive. How to call the Parent Activity without finishing the child Activity(in other words, How to BringtoFront the parent Activity)? For this scenario,

  • you can not use startActivity(). Because it creates a new Activity Instance. Not Existing Parent Activity
  • you can not use startActivityForResult(). Because it finishes the child Activity Instance.

Is that possible in Android? Any Idea?


Solution

  • This is not possible in Android. Such a case is not provided for in the activity life cycle.

    It is possible to simulate such a behaviour through reusing a single activity and just switching the layout. You would have to store the whole state of the activity, then change the layout to the layout that should be the child activity and if the user switches back to the parent activity you would have to save the state of the children activity and restore the state of the parent activity. You would also need to handle the back key yourself to prevent the user to press it and go back to the activity that started the parent activity.

    A simpler way would it be to use a tabwidget and let the user switch between to tabs. This enables you to initialize the tabs with two different intents and both of them will be only paused if the user changes back to another tab.