Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult() not called

onActivityResult() is not getting called. Below is my code:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     // Check which request we're responding to      Log.e("CALLED", "OnActivity Result");      if (requestCode == TEAM_SELECTED_REQUEST_CODE) {         // Make sure the request was successful         if (resultCode == RESULT_OK) {             try {                  mySelectedTeam = getIntent().getStringExtra("teamName");                 txtSelectTeamCreateMatch.setText(mySelectedTeam);             } catch (Exception e) {                 e.printStackTrace();             }         }     } } 

Here's I'm starting the SelectTeamActivity:

Intent intent=new Intent(CreateMatch.this,SelectTeamActivity.class); startActivityForResult(intent, TEAM_SELECTED_REQUEST_CODE); //overridePendingTransition(R.anim.fade_in, R.anim.fade_out); overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);  Intent intent = getIntent(); intent.putExtra("teamID", teamDataList.get(position).getTeamId().toString()); intent.putExtra("teamName", teamDataList.get(position).getTeamName().toString()); setResult(1, intent); 
like image 714
Parin Parikh Avatar asked Nov 18 '14 06:11

Parin Parikh


People also ask

How can we call fragment onActivityResult from activity?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity(). startActivityForResult() from your fragment.

How can I get result from startActivityForResult?

First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .

What is the use of onActivityResult in Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.


2 Answers

Option 1 :

If you're calling startActivityForResult() from the Fragment then you should call startActivityForResult() and not getActivity().startActivityForResult(), as it will result in Fragment's onActivityResult().

If you're not sure where you're calling on startActivityForResult() and how you will be calling methods.

Option 2:

Since Activity gets the result of onActivityResult(), you will need to override the Activity's onActivityResult() and call super.onActivityResult() to propagate to the respective Fragment for unhandled results codes or for all.

If above 2 options do not work, then refer option 3 as it will definitely work.

Option 3 :

Explicit call from Fragment to onActivityResult() function as follows

In parent Activity class, override the onActivityResult() and even override the same in Fragment class and call as the following code.

In parent class:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);     fragment.onActivityResult(requestCode, resultCode, data); } 

In child class:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {    //in fragment class callback } 
like image 166
Akash Moradiya Avatar answered Oct 03 '22 03:10

Akash Moradiya


I solved my problem by removing android:noHistory="true" from AndroidManifest.xml.

like image 39
Android Dev Avatar answered Oct 03 '22 03:10

Android Dev