简体   繁体   中英

Pass data from interface in Activity to another Activity

I try pass value from Fragment to Activity using interface:

 public interface OnSelectedButtonListener {
    void onButtonSelected(int buttonIndex);
}

This is working, but when I try pass value from interface in Activity to another Activity I get null. How to resolve this problem in another way?

@Override
public void onButtonSelected(int buttonIndex) {
    Intent intent = new Intent(MainActivity.this, ArticleActivity.class);
    intent.putExtra("text",buttonIndex);
    startActivity(intent);
}

check this lesson Starting Another Activity

MainActivity

At the top of the MainActivity class, add the EXTRA_MESSAGE definition as follows:

public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

and call the start activity:

Intent intent = new Intent(MainActivity.this, ArticleActivity.class);
intent.putExtra(EXTRA_MESSAGE, buttonIndex);
startActivity(intent)

ArticleActivity

Inside OnCreate(Bundle savedInstanceState) get the intent and assign it to a local variable.

Intent intent = getIntent();
int index;
if (intent != null) {
   index = intent.getIntExtra(MainActivity.EXTRA_MESSAGE, 0);
}

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.

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