Search code examples
androidandroid-intentfragmentpageradapterstartactivityforresult

Intent lost on BACK button even if I try startActivityForResult, saveInstanceState in FragmentPagerAdapter


I have this navigation schema in my android application:

PrimaryActivity (... ListView onClick - sending <PrimaryUri> via intent...) >
MasterActivity (TabLayout, ViewPager, FragmentPageAdapter) >
FragmentA / FragmentB (...ListView onClick - sending <PrimaryUri> + <DetailUri> via intent...) > DetailActivity.

Now when I press BACK button in DetailActivity, MasterActivity gets destroyed and recreated again with new instances of Fragment A and B and Intent data (<PrimaryUri>) is lost.

I tried persisting <PrimaryUri> between Activies by this way:

  • override onSaveInstanceState / onCreate(Bundle) in MasterActivity, but Bundle was always null even onSaveInstance saves the data
  • start DetailActivity with getActivity().startActivityForResult(..., set resultIntent in DetailActivity and call setResult(OK, ResultCode > 0) and read values in MasterActivity.onActivityResult... But onActivityResult was never called
  • start DetailActivity with startActivityForResult, ...dtto...FragmentB.onActivityResult... But onActivityResult is never called
  • checked manifest for wrong attributes

Now I really appreciate help for question: How to persist <PrimaryUri> in my application? I prefer to use intents and solve this strange behaviour. Some forum discussed errors in FragmentPageAdapter etc... Maybe I will refactor it for use of SharedPreferences if it will be quick solution...

EDIT: Some additional info:

In DetailActivity:

@Override  
public void finish() {  
    Intent resultIntent = new Intent();  
    resultIntent.setData((Uri) getIntent().getParcelableExtra(FragmentB.MY_URI));  
    setResult(Activity.RESULT_OK, resultIntent);  
    super.finish();  
}  

In MasterActivity I have:

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

In FragmentB I have:

public void onItemSelected(Uri contentUri) {      
    Intent intent = new Intent(getActivity(), DetailActivity.class);     
    intent.putExtra(PRIMARY_URI, mUri);...     
    startActivityForResult(intent, FragmentB.BACKTAG);  
} 

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {   
    super.onActivityResult(requestCode, resultCode, data);    
    switch (requestCode) {  
        case (FragmentB.BACKTAG): {  
            if (resultCode == Activity.RESULT_OK) {  
                mUri = data.get...  
            }  
        }  
        break;  
    }    
}  

The strange behavior looks like:

  1. I press BACK button in DetailActivity >
  2. MasterActivity.onDestroy get called (why there?).. =
  3. WeakReference to FragmentB is null and new instance is created =
  4. onActivityResult of MasterActivity or FragmentB is never called.

I think that reason is that onDestroy appears while waiting for onActivityResult. But why it is called and why the MasterActivity isn't fully recreated to catch onActivityResult callback?


Solution

  • This was headache, but here I found the solution after 2 days of futile attempts. This has nothing to do with FragmentPageAdapter. I am lucky woman!