How to get parent activity of name in child activity

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

where I add objects to the ArrayList that causes the activity to start my THREE activity. How can i do this?

+3


source to share


7 replies


Something like that

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 activity names. Later you will want to change the class name, add a new one, etc. You will need to make a lot of changes and the code is difficult to maintain. It is better to introduce an operating mode, and any other activity will lead to the fact that your activity will work in a certain 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;
     }

      

+5


source


You must add an extra value to the intent that triggers this THREE activity.

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

      



When reading, do the following:

getIntent().getStringExtra("prevActivity");

      

+3


source


one easy way to do this is to add a context element to your intent and pass the 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");

      

+2


source


Just a recognizable piece of data is passed in the intent ...

+1


source


Put your Actvity name in the Intent and pass it ... but never try to use it to update the UI or anything since it was suspended in a state. An exception..

+1


source


You can accomplish your task by creating Interface

as Discused here or you can accomplish the task by putExtra as

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

      

And then get in the form of a child

String parentActivityName = intent.getStringExtra("PARENT_ACTIVITY_NAME");

      

0


source


You can also use getIntent().getClass().getName()

to extract it.

0


source







All Articles