1

Is there any way to change the parent of an activity from class file.
I have two activities say ONE and TWO, and both of them can start a new activity say THREE.

Now if ONE starts THREE, then for up Navigation parent should be ONE.
But if TWO starts THREE, then for up Navigation parent should be TWO.

If i can change the parent for an activity from my class file. I will send some data from activities (ONE and TWO) to uniquely identify them then set the parent according to that data.

If there is any other way to do this then let me know.

Ankesh Kushwah
  • 161
  • 1
  • 3
  • 12
  • can you tell why do you want to change parent? – DroidDev Nov 11 '13 at 11:06
  • Let say, at start ONE(activity) is the parent of THREE(activity). But i start THREE from TWO now if user clicks on up button then app will stop working cause **up button** was set for ONE not for TWO(means there was no working instance of activity ONE). If i change the parent of THREE, to TWO, then pressing up button will return the control to the instance of TWO which is exist. – Ankesh Kushwah Nov 11 '13 at 11:14

2 Answers2

0

I think you can only have one defined parent in your manifest but you could just send your parent when you launch your activity. Just pass a bundle with what activity launched your Third activity and handle it in OnBackPressed and if you want to change your up button handle it in onOptionsItemSelected.

Pontus Backlund
  • 1,017
  • 1
  • 10
  • 17
0

In android activities are added in stack as they are started. Having said that, when you start activity THREE from activity TWO, activity TWO is added in stack below THREE. Now on click of up button in activity THREE, you can finish activity THREE and the control will automatically return to activity TWO.

Please follow this post for getting up button press in android. Now in its onClick case, you just need to finish activity THREE, and the control will return to whichever activity you came from. Whether it be ONE or TWO. You can finish the activity in onClick as following:- Just write

finish();

in the case for home button

Community
  • 1
  • 1
DroidDev
  • 1,527
  • 4
  • 20
  • 42