Android 如何将消息从片段发送到活动并在活动中接收和使用?

Android 如何将消息从片段发送到活动并在活动中接收和使用?,android,android-fragments,android-activity,message,Android,Android Fragments,Android Activity,Message,请不要减去我在谷歌搜索时弄糊涂的问题。 我在代码中使用了当用户按下活动上的设置按钮时 现在我需要从TopRatedFragment.java发送消息,该消息从片段扩展到调用“带可切换视图的Android选项卡布局”主活动的活动。仔细查看此参考: android文档建议使用这种模式,让父活动实现片段的接口(基本上是调用片段上的方法) 这里用一个更具体的例子解释:这是解决方案: 步骤1:从片段中 Intent i = new Intent(getActivity(), YourActivity.cl

请不要减去我在谷歌搜索时弄糊涂的问题。 我在代码中使用了当用户按下活动上的设置按钮时


现在我需要从TopRatedFragment.java发送消息,该消息从片段扩展到调用“带可切换视图的Android选项卡布局”主活动的活动。

仔细查看此参考:
android文档建议使用这种模式,让父活动实现片段的接口(基本上是调用片段上的方法)


这里用一个更具体的例子解释:

这是解决方案:

步骤1:从片段中

Intent i = new Intent(getActivity(), YourActivity.class);
        i.putExtra("key", "Your value1");
        i.putExtra("key2", "Your value2");
        i.putExtra("key3", "Your value3");
        getActivity().startActivity(i);
步骤2:在活动中,您希望得到结果

Intent getResults = getIntent();
        String firstValue = getResults.getStringExtra("key1");
        String secondValue = getResults.getStringExtra("key2");
        String thirdValue = getResults.getStringExtra("key3");
使用你需要的价值观


希望这有帮助:)

您可以通过实现回调来实现这一点

首先创建一个接口

public interface CommunicationInterface {

public void onSuccess();

public void onFailed();

}
然后在活动中实现接口

public class YourActivity extends ActionBarActivity implements  CommunicationInterface {

 //default functions
@Override
public void onSuccess() {

  //stuff you want to do in the acivity

}

@Override
public void onFailed() {
   //stuff you want to do in the acivity
}

}
现在在片段中

public class yourfragment extends Fragment {

 CommunicationInterface callback;

 //stuffs that usually come in yor fragment and like OncreateView etc

@Override
public void onActivityCreated(@Nullable Bundle outState) {
    super.onActivityCreated(outState);

   //after all the stuff you want to do in your fragment then implement      //call back function to communicate with the activity
   callback= (CommunicationInterface) getActivity();

    callback.onSuccess();//according to your purpose use where ever you like

    callback.onFailed();//according to your purpose use where ever you like

}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
  callback= (CommunicationInterface) activity;
}
}

这假设总是可以调用
getActivity
。在某些情况下,该函数返回
null
@Christian。在这种情况下,初始化onAttach()方法的回调,它将为您提供活动的上下文。
public class yourfragment extends Fragment {

 CommunicationInterface callback;

 //stuffs that usually come in yor fragment and like OncreateView etc

@Override
public void onActivityCreated(@Nullable Bundle outState) {
    super.onActivityCreated(outState);

   //after all the stuff you want to do in your fragment then implement      //call back function to communicate with the activity
   callback= (CommunicationInterface) getActivity();

    callback.onSuccess();//according to your purpose use where ever you like

    callback.onFailed();//according to your purpose use where ever you like

}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
  callback= (CommunicationInterface) activity;
}
}