流量如何?
Activity 1 -----> Activity 2 (containing/inside) ------> Fragment
我想要实现的目标?
Fragment (sends some data back to Activity 2) ----> Activity 2 (onBackPressed : collects that data & send it back to Activity 1) ---> Activity 1
我应该如何实现上述目标。我真的不想使用任何变量/常量来缓存片段数据。需要知道任何内置方法来处理这个问题吗?
此外,
答案 0 :(得分:1)
您应该使用startActivityForResult启动Activity2,如下所示;
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, requestCode);
在Activity2 / fragment中,您应该完成以下活动;
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
getActivity().setResult(Activity.RESULT_OK,returnIntent);
getActivity().finish()
并在Activity1中得到如下结果;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (this.requestCode == requestCode) {
if(resultCode == Activity.RESULT_OK){
//Get result
}
}
}
希望它有所帮助。
答案 1 :(得分:0)
要将数据从Activity 2
传递到Activity 1
,您可以使用startActivityForResult()
中的Activity 1
开始Activity 2
,onActivityResult()
Activity 1
接收该数据。
要将数据从Fragment
传递到Activity 2
,我建议使用某些界面,例如
interface OnSomeDataListener {
void onSomeData(SomeData data);
}
然后将此接口的setter方法实现为像
这样的片段public void setOnSomeDataListener(OnSomeDataListener listener) {
this.listener = listener;
}
然后在Activity中,实例化Fragment
:
YourFragment fragment = new YourFragment();
fragment.setOnSomeDataListener(new OnSomeDataListener() {
void onSomeData(SomeData data) {
//return the result
Intent intent = new Intent();
intent.putExtra("data", data);
setResult(RESULT_OK, intent);
finish();
}
}
最后,在片段中,当您想要将一些数据返回到Activity:
if(listener != null) {
listener.onSomeData(dataToReturn);
}
答案 2 :(得分:0)
我按照以下方式实现
在Activity中写入setter和getter
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
并在Fragment中
filePath = ((YourActivity) getActivity()).getFilePath();
fileName=((YourActivity) getActivity()).getFileName();
如果您在多个活动中使用相同的片段您可以为该片段创建构造函数并传递上下文并检查上下文是哪个活动
public class BookmarkFragment extends Fragment {
private String filePath;
private String fileName;
Context contextCheckClass;
public BookmarkFragment(Context ctx ) {
this.contextCheckClass=ctx;
}
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
group= (View)inflater.inflate(R.layout.fragment_bookmark, null);
if(contextCheckClass instanceof FirstReaderScreen){
filePath = ((FirstReaderScreen) getActivity()).getFilePath();
fileName=((FirstReaderScreen) getActivity()).getFileName();
// ispurchased=((FirstReaderScreen) getActivity()).isIspurchased();
}
else if(contextCheckClass instanceof MainReaderScreen){
filePath = ((MainReaderScreen) getActivity()).getFilePath();
fileName=((MainReaderScreen) getActivity()).getFileName();
// ispurchased=((MainReaderScreen) getActivity()).isIspurchased();
}
return group;
}
用于调用片段
BookmarkFragment bookmarkFragment=new BookmarkFragment(FirstReaderScreen.this);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, bookmarkFragment)//LL_fragment is container
.addToBackStack(null)
.commit();
答案 3 :(得分:0)
从FirstActivity使用startActivityForResult()方法调用SecondActivity
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
在SecondActivity中,设置要返回FirstActivity onBackPressed的SecondActivity数据。
@Override
public void onBackPressed() {
super.onBackPressed();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
returnIntent.putExtra("bool",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
现在,在您的FirstActivity类中,编写onActivityResult()方法的以下代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
boolean bool = data.getBooleanExtra("bool");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//
要将数据从Fragment发送到Second活动,您可以使用interface callback
public interface sendDataListener
{
void sendData(boolean foo);
}
将此侦听器实现为第二个活动
答案 4 :(得分:0)
尝试这样做:
<build>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
</build>
然后声明你的界面:
public class MyFragment extends Fragment{
private MyFragmentCommunicator myFragmentCommunicator;
....
myFragmentCommunicator.sendDataToActivity(name, image, isSend);
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
myFragmentCommunicator = (MyFragmentCommunicator)activity;
}
}
然后在你的Activity中执行此操作:
public interface MyFragmentCommunicator{
void sendDataToActivity(String name, String image, boolean isSend);
}
希望它有所帮助!!!