Is it possible for one Activity
to have two others. What I mean is I have 2 buttons for two parent activities - Button 1
and Button 2
. When I click on Button 1
it is opening activity 1
which also has activity 2
. When I click Button 2
it is opening same activity 1
like on Button 1
and here is the catch. I want 3rd activity of button 2 to be different. Currently it opens 3rd activity which is on button 1. I will try to illustrate it
Button 1(Activity 1) -> listView (Activity 2) -> TextView (Activity 3)
Button 2(Activity 1) -> listView (Activity 2) -> TextView (Activity 3) <- this must be different from above activity 3
I use same Activity 2 for both menu buttons and then activity 3 must be different for both. I cansee from where this problem come. Because second activity on button 2 is the second activity on button 1. But I don't want to make another activity and to load same thing and to duplicate code/activities. Or should I? Update:
Button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
intent.putExtra("button", "button1");
startActivity(intent);
}
});
Button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
intent.putExtra("button", "button2");
startActivity(intent);
}
});
Here is activity 3 for button1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act3);
Bundle b = getIntent().getExtras();
if (b != null) {
String button = b.getString("button");
}
}
Here is activity 3 for button2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act3_1);
Bundle b = getIntent().getExtras();
if (b != null) {
String button = b.getString("button");
}
}
Use this for button 1 onCLick
:
intent.putExtra("button", "button1");
For button 2 onCLick
:
intent.putExtra("button", "button2");
Pass that value through from Activity
1 to Activity
3, through Activity
2.
In Activity
3's onCreate
, use :
Bundle b = getIntent().getExtras();
if(b != null)
String btn = b.getString("button");
You can use this String
to know, whether its from button1 or button2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.