简体   繁体   English

将数据从Activity中的接口传递到另一个Activity

[英]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.

相关问题 将数据从 Activity 传递到另一个 Activity 中的 Fragment - Pass data from Activity to Fragment in another Activity 如何将数据从活动传递到另一个活动,然后再传递到Android上的另一个活动? - How to pass data from an activity to another activity and then to another activity on Android? 使用接口将数据从活动传递到片段 - Pass Data From Activity To Fragment Using Interface 如何从一个活动的列表视图传递数据并将其传递给另一活动? - How to pass the data from listview of one activity and pass it to another activity? 如何将数据从一个活动传递到另一活动 - How to pass data from one activity to another activity 如何将数据从一个活动传递到 Android 中的另一个活动 - How to pass data from one activity to another activity in Android 如何将图像数据从一个活动传递到另一个活动? - How to pass image data from one activity to another activity? 将数据从一个活动传递到另一个活动列表视图 - Pass data from one activity to another activity listview 如何使用SharedPreferences将数据从活动传递到另一个活动 - how to use SharedPreferences to pass data from activity to another activity 我如何将数据从一个活动传递到另一活动 - how i pass data from one activity to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM