[英]Pass data from interface in Activity to another Activity
I try pass value from Fragment
to Activity
using interface: 我尝试使用接口将值从
Fragment
传递到Activity
:
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. 这是可行的,但是当我尝试将值从
Activity
接口传递给另一个Activity
我得到了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: 在MainActivity类的顶部,添加EXTRA_MESSAGE定义,如下所示:
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 ArticleActivity
Inside OnCreate(Bundle savedInstanceState) get the intent and assign it to a local variable. 在OnCreate(Bundle savedInstanceState)内部,获取意图并将其分配给局部变量。
Intent intent = getIntent();
int index;
if (intent != null) {
index = intent.getIntExtra(MainActivity.EXTRA_MESSAGE, 0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.