简体   繁体   English

从一项活动到另外两项活动

[英]From one activity to two other

Is it possible for one Activity to have two others. 一个Activity可能有另外两个Activity吗? What I mean is I have 2 buttons for two parent activities - Button 1 and Button 2 . 我的意思是我有两个用于两个父级活动的Button 1 - Button 1Button 2 When I click on Button 1 it is opening activity 1 which also has activity 2 . 当我单击Button 1它正在打开activity 1 ,其中也有activity 2 When I click Button 2 it is opening same activity 1 like on Button 1 and here is the catch. 当我单击Button 2它会像在Button 1上一样打开相同的activity 1 Button 1 ,这就是问题。 I want 3rd activity of button 2 to be different. 我希望按钮2的第3个活动有所不同。 Currently it opens 3rd activity which is on button 1. I will try to illustrate it 当前,它打开按钮1上的第3个活动。我将尝试说明它

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. 我对两个菜单按钮都使用了相同的活动2,然后活动3必须对两者都不同。 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. 因为按钮2上的第二个活动是按钮1上的第二个活动,但是我不想进行另一个活动并加载相同的东西并复制代码/活动。 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 这是button1活动3

    @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 这是button2活动3

@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 : 将此按钮用于onCLick按钮1:

intent.putExtra("button", "button1");

For button 2 onCLick : 对于点击2的onCLick

intent.putExtra("button", "button2");

Pass that value through from Activity 1 to Activity 3, through Activity 2. 通过Activity 2将该值从Activity 1传递到Activity 3。

In Activity 3's onCreate , use : Activity 3的onCreate ,使用:

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. 您可以使用此String知道它是来自button1还是来自button2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM