edi233
edi233

Reputation: 3031

How get name parent activity in child activity

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?

Upvotes: 3

Views: 8390

Answers (7)

Divya Jampala
Divya Jampala

Reputation: 1

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

Upvotes: -1

Inzimam Tariq IT
Inzimam Tariq IT

Reputation: 6748

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");

Upvotes: 0

Ion Aalbers
Ion Aalbers

Reputation: 8030

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");

Upvotes: 3

Vyacheslav Shylkin
Vyacheslav Shylkin

Reputation: 9791

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;
     }

Upvotes: 5

Mayank
Mayank

Reputation: 8852

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");

Upvotes: 2

ngesh
ngesh

Reputation: 13501

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..

Upvotes: 1

jtt
jtt

Reputation: 13541

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

Upvotes: 1

Related Questions