startActivityForResult() from a Fragment and finis

2019-01-04 06:35发布

FirstActivity.Java has a FragmentA.Java which calls startActivityForResult(). SecondActivity.Java call finish() but onActivityResult never get called which is written in FragmentA.Java.

FragmentA.Java code:

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // some code
    Intent i = new Intent(getActivity(), SecondActivity.class);
    i.putExtra("helloString", helloString);
    getActivity().startActivityForResult(i, 1);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    getActivity();
    if(requestCode == 1 && resultCode == Activity.RESULT_OK) {
       //some code
    }
  }

SecondActivity.Java code:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //some code
       Intent returnIntent = new Intent();
       returnIntent.putExtra("result", result);                          

       setResult(Activity.RESULT_OK,returnIntent);     
       finish();
  }

I have tried debugging the code, but onAcitvityResult() never get called.

9条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-04 06:57

You must write onActivityResult() in your FirstActivity.Java as follows

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

This will trigger onActivityResult method of fragments on FirstActivity.java

查看更多
神经病院院长
3楼-- · 2019-01-04 06:57

The fragment already has startActivityForResult, which would call onActivityResult in the fragment if you use it, instead of getActivity()...

查看更多
一夜七次
4楼-- · 2019-01-04 07:04

We could call startActivityForResult() directly from Fragment So You should call this.startActivityForResult(i, 1); instead of getActivity().startActivityForResult(i, 1);

Intent i = new Intent(getActivity(), SecondActivity.class);
i.putExtra("helloString", helloString);
this.startActivityForResult(i, 1);

Activity will send the Activity Result to your Fragment.

查看更多
冷血范
5楼-- · 2019-01-04 07:11

onActivityResult() of MAinActivity will call , onActivityResult() of Fragement wont call because fragment is placed in Activity so obviously it come back to MainActivity

查看更多
放我归山
6楼-- · 2019-01-04 07:18

Kevin's answer works but It makes it hard to play with the data using that solution.

Best solution is don't start startActivityForResult() on activity level.

in your case don't call getActivity().startActivityForResult(i, 1);

Instead, just use startActivityForResult() and it will work perfectly fine! :)

查看更多
一夜七次
7楼-- · 2019-01-04 07:18

The most important thing, that all are missing here is... The launchMode of FirstActivity must be singleTop. If it is singleInstance, the onActivityResult in FragmentA will be called just after calling the startActivityForResult method. So, It will not wait for calling of the finish() method in SecondActivity.

So go through the following steps, It will definitely work as it worked for me too after a long research.

In AndroidManifest.xml file, make launchMode of FirstActivity.Java as singleTop.

<activity
        android:name=".FirstActivity"
        android:label="@string/title_activity_main"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar" />

In FirstActivity.java, override onActivityResult method. As this will call the onActivityResult of FragmentA.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

In FragmentA.Java, override onActivityResult method

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("FragmentA.java","onActivityResult called");
}

Call startActivityForResult(intent, HOMEWORK_POST_ACTIVITY); from FragmentA.Java

Call finish(); method in SecondActivity.java

Hope this will work.

查看更多
登录 后发表回答