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?
source to share
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;
}
source to share
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");
source to share