3

How I can get name of parent activity in child activity. I have two activities where I can start the same activity. For better understanding: I have activity ONE, TWO, THREE. From activity ONE I can start activity THREE and from activity TWO I can start activity THREE. Now I have a question. How I can get in activity THREE, name of parent activity.So when I start activity THREE from activity ONE how I can get this information. I want to implement simple loop if() where I add objects to the ArrayList due to which activity starts my activity THREE. How I can do that?

edi233
  • 3,511
  • 13
  • 56
  • 97
  • http://stackoverflow.com/questions/3410295/finish-parent-and-current-activity-in-android – Prem Mar 27 '12 at 08:45
  • If you are using the `android:parentActivityName` attribute in the manifest, then this answer may help: http://stackoverflow.com/a/38088425/1617737 . – ban-geoengineering Jun 29 '16 at 08:06

7 Answers7

5

Something like this

FirstActivity:

     Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
     intent.putExtra(Consts.PARENT_ACTIVITY_NAME, "ONE");
     ...
     startActivity(intent);

SecondActivity:

     Intent intent = getIntent();
     String parentName = intent.getStringExtra(Consts.EPARENT_ACTIVITY_NAME;
     if(parentName.equals(...)){ 
        ....
     }

But in my opinion it is better not to use the names of the activities. Later you'll want to change the name of the class, add new, etc. You will have to make a lot of edits and the code is hard to maintain. It is better to enter the mode of operation, and all other activity will cause your activity to a specific mode. So:

FirstActivity:

     Intent intent = new Intent(FirstActivity.this, SomeActivity.class);
     intent.putExtra(Consts.PARENT_ACTIVITY_MODE, TwoActivity.MODE_EDIT);
     ...
     startActivity(intent);

SecondActivity:

     Intent intent = new Intent(SecondActivity.this, SomeActivity.class);
     intent.putExtra(Consts.PARENT_ACTIVITY_MODE, TwoActivity.MODE_DELETE);
     ...
     startActivity(intent);

ThirdActivity:

     Intent intent = new Intent(ThirdActivity.this, SomeActivity.class);
     intent.putExtra(Consts.PARENT_ACTIVITY_MODE, TwoActivity.MODE_COPY);
     ...
     startActivity(intent);

Some activity:

     Intent intent = getIntent();
     int mode = intent.getIntExtra(Consts.EPARENT_ACTIVITY_MODE);
     switch(mode){
         case MODE_EDIT:
              ....
              break;
         case MODE_DELETE:
              ....
              break;
         case MODE_COPY:
              ....
              break;
     }
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34
3

You should put an extra to the intent that is starting up that THREE activity.

intent.putExtra("prevActivity", "ONE");

When reading do this:

getIntent().getStringExtra("prevActivity");
Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
2

one easy way to do it would be to add an context element to your intent and passing name of the activity

Intent in = new Intent(One.this, Three.class);
in.putExtra("activity", "One");
startActivity(in);

and then in Three Activity

this.getIntent().getExtras().getString("activity");
Mayank
  • 8,777
  • 4
  • 35
  • 60
1

Just passs a recognizable piece of data in the intent...

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
1

Put your Actvity name in Intent and pass it... but never try to Use it to update UI or something since its in Paused state.. It will throw Exception..

ngesh
  • 13,398
  • 4
  • 44
  • 60
0

You can Complete your task by Creating an Interface as Discused here or you can do the job by putExtra as

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
     intent.putExtra("PARENT_ACTIVITY_NAME", "ThisActivityName");
     startActivity(intent);

And then receive in child like

String parentActivityName = intent.getStringExtra("PARENT_ACTIVITY_NAME");
Community
  • 1
  • 1
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
-1

You can also use getIntent().getClass().getName() to fetch it.

ikos23
  • 4,879
  • 10
  • 41
  • 60