简体   繁体   中英

Android rotation after startActivityForResult

I have 2 activities, MainActivity (contains MainFragment ) and SearchActivity .

When the user clicks the search button in MainActivity , I open SearchActivity using startActivityForResult() . Then the user types in their query and I return that query as the result to MainActivity where I have a searcher object that actually performs a search with the query.

I'm running into an issue in the following circumstance, though: I am in MainActivity and click the search button. I go to SearchActivity where I proceed to rotate the phone. I type in my query and return to MainActivity where it crashes because searcher is null.

So I understand that when I rotate the screen MainActivity is getting destroyed and recreated, and I am saving the instance state, the problem I'm having is that onActivityResult() is getting called before onActivityCreated() in MainFragment . How can I ensure that MainFragment gets restored to its previous state before I handle the result?

EDIT:

Here is my (heavily edited; its for work) code.

Main Activity

public class MainActivity extends AppCompatActivity {
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mainFragment = getSupportFragmentManager().getFragment(savedInstanceState, "fragment");
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, "fragment", mainFragment);
    }
}

Main Fragment

public class MainFragment extends Fragment {
    private Searcher searcher;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        searcher = new Searcher();

        if (savedInstanceState != null) {
            // restore state
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (resultCode == Activity.RESULT_OK) {
             if (data.hasExtra("query")) {                
                 String query = data.getStringExtra("query");
                 searcher.search(query);
             }
         }
     }

     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         // save state
     }
}

For example when I have troubles with WebView I have done this:

@Override
protected void onSaveInstanceState(Bundle outState ) {
    super.onSaveInstanceState(outState);
    mWebView.saveState(outState); //Do your own action
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mWebView.restoreState(savedInstanceState); // Do your own action
}

And this may help with crashes:

    if (savedInstanceState == null)
    {
        mWebView.loadUrl("https://goo.gl/6wE4Di"); //do your own action
    }

add

android:configChanges="orientation|screenSize"

in your MainActivity tag in Android Manifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM