0

I am trying to startActivityForResult in my fragment and i do this way:

public class MyFragment extends Fragment {
.....
 new GetNidBase(getContext(), code, Integer.parseInt(_newBlock.split("-")[3]),
     WKT1, user, cookie, permissions, _newBlock, (nidBase) -> {
     if (!TextUtils.isEmpty(nidBase)) {
         Intent intent = new Intent(requireContext(), FormSaveDocument.class);
         intent.putExtra(BaseFragment.ARG_COOKIE, HttpUtil.serializeCookie(cookie));
         intent.putExtra(BaseFragment.ARG_USER, common.Utils.compactUserInstance(user));
         intent.putExtra(BaseFragment.ARG_PERMISSIONS, GsonUtil.createGson().toJson(permissions));
         startActivityForResult(intent, INTENTRESULT);
         }

I want to handle my result in fragment's onActivityForResult() But i wondered that, FormSaveDocument is not started? FormSaveDocument is a activity class!!startActivityForResult(intent, INTENTRESULT) not work.

I also trying this:

MyFragment.this.startActivityForResult(intent, INTENTRESULT);

This is onActivityResult in my fragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == INTENTRESULT && resultCode == 20) {
        saveDraw();
    }
}

Update

This is myActivity and FormSaveDocument manifest info:

   <activity
        android:name=".activity.MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    <activity
        android:name=".forms.FormSaveDocument"
        android:configChanges="keyboardHidden|orientation|screenSize" />

    </activity>
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149
  • Is this a typo? `resultCode == 20` – elbert rivas Apr 15 '19 at 05:49
  • have you tried https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment – akshay_shahane Apr 15 '19 at 05:50
  • are you using context from parent activity? I'm assuming that `requireContext()` is getting the context from parent activity. Is there any error logs? If there are any logs, please add some logs. – Amol Gangadhare Apr 15 '19 at 05:51
  • I am calling startActivityForResult() from the fragment then i should call startActivityForResult() and i do it ... @akshay_shahane – Cyrus the Great Apr 15 '19 at 05:51
  • Possible duplicate of [startActivityForResult() from a Fragment and finishing child Activity, doesn't call onActivityResult() in Fragment](https://stackoverflow.com/questions/17085729/startactivityforresult-from-a-fragment-and-finishing-child-activity-doesnt-c) – behrooz Apr 15 '19 at 05:52
  • "But i wondered that, `FormSaveDocument` is not started?" – If you mean that `FormSaveDocument` is not showing at all, are you sure that that code is executing? – Mike M. Apr 15 '19 at 05:53
  • are you using context from parent activity? If course, `requireContext()` comes from parent @AmolG – Cyrus the Great Apr 15 '19 at 05:54
  • make sure your Host Activity launch mode must not set to singleInstance or singleTask – akshay_shahane Apr 15 '19 at 05:56
  • In activity i do : `myFragment.setArguments(args); getSupportFragmentManager().beginTransaction() .addToBackStack(MyFragment.getClass().getName()) .replace(R.id.fragment_holder, myFragment) .commit();` @akshay_shahane – Cyrus the Great Apr 15 '19 at 05:58
  • is there any error logs? does the activity is able to start from other ways without any error? – Amol Gangadhare Apr 15 '19 at 05:59
  • I do not have any error and logcat is not showing any error and i did not get any error.Just do not started my activity@AmolG – Cyrus the Great Apr 15 '19 at 06:03
  • Share manifest. – Pankaj Kumar Apr 15 '19 at 06:05
  • 1
    Do you have an `` element for `FormSaveDocument`? – Mike M. Apr 15 '19 at 06:21
  • @MikeM. ops i forgot to add .. I updated my question – Cyrus the Great Apr 15 '19 at 06:23
  • It's really starting to sound like that `startActivityForResult()` line isn't being reached. You might try setting a breakpoint there, or put a log print, or something, just to make sure. – Mike M. Apr 15 '19 at 06:25
  • is it because of !TextUtils.isEmpty(nidBase) always false? – akshay_shahane Apr 15 '19 at 06:27
  • I am sure the cursor reach to `startActivityForResult()` and pass from it but `FormSaveDocument` not started and i got any error @MikeM – Cyrus the Great Apr 15 '19 at 06:27
  • I am taking a picture from break point to see cursor reach @MikeM. – Cyrus the Great Apr 15 '19 at 06:29
  • This is my debug photo : http://pasteall.org/pic/show.php?id=ceb599c19f7188bfa43f0b8ee41e00d2 @MikeM. – Cyrus the Great Apr 15 '19 at 06:33
  • Well, if `FormSaveDocument` is failing when starting up, there really should be something in [your logcat](https://developer.android.com/studio/debug/am-logcat#running). If you're not seeing anything, you might post `FormSaveDocument` in your question, to see if we notice anything that could be causing an issue. – Mike M. Apr 15 '19 at 06:35
  • I am trying to check deeper ..Thanks so much @MikeM – Cyrus the Great Apr 15 '19 at 06:37

2 Answers2

0

In Your Activity Where you get your result back you have to use same object of fragment which you used to add Fragment.

Dipakk Sharma
  • 976
  • 1
  • 8
  • 11
0

Please use
startActivityForResult(intent, INTENTRESULT); insted of call MyFragment.this.startActivityForResult(intent, INTENTRESULT); from your fragment, and

Then after handle onActivityResult() in your fragment

This will give you call back in your fragment

Another Solution is:
Handle onActivityResult in your activity

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            getCurrentFrg().onActivityResult(requestCode,resultCode,data);    
        }catch (Exception e){
            e.printStackTrace();
        }
    }
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
  • dude, I said above that I also tried `MyFragment.this.startActivityForResult` but first i use `startActivityForResult` and i did not get answer – Cyrus the Great Apr 15 '19 at 06:05
  • Okay, Then the final solution is called as you calling and handle onActivityResult in your activity and call fragment onActivityResult from the activity callBack – Dhaval Solanki Apr 15 '19 at 06:15
  • dude my problem is `startActivityForResult`, not ` onActivityResult` @DhavalSolanki – Cyrus the Great Apr 15 '19 at 06:22