How to handle orientation change in case of single activity and multiple fragments

246
August 06, 2017, at 3:41 PM

In my app, I have a single Activity and multiple Fragments. From my MainActivity, I add my first Fragment. From inside the first Fragment adapter based on item click, I add the second Fragment. And from second Activity adapter based on item click, I add the third Fragment.

Everything works well but on orientation change, the app crashes.

Code:

MainActivity:

if (savedInstanceState == null) {
    mHomeScreenFragment = (HomeScreenFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame);
    if (mHomeScreenFragment == null) {
        mHomeScreenFragment = HomeScreenFragment.newInstance();
    }
    ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), mHomeScreenFragment, R.id.contentFrame, "HomeScreen");
}

1st Fragment adapter - on item click:

FoodListFragment foodListFragment = null;
if (foodListFragment == null) {
   foodListFragment = foodListFragment.newInstance(position);
    ActivityUtils.replaceFragmentToActivity(((MainActivity) view.getContext()).getSupportFragmentManager(),
                                foodListFragment, R.id.contentFrame, "FoodList");

}

2nd Fragment adapter - on item click:

FoodDetailsFragment foodDetailsFragment = null;//(FoodDetailsFragment) ((MainActivity)view.getContext()).getSupportFragmentManager().findFragmentByTag("FoodDetails");
if (foodDetailsFragment == null) {
    foodDetailsFragment = foodDetailsFragment.newInstance(foodListData.getUrl(),
                                                          foodListData.getName(),
                                                          foodListData.getSalePrice().getAmount(),
                                                          foodListData.getSalePrice().getCurrency());
}
ActivityUtils.replaceFragmentToActivity(((MainActivity)view.getContext()).getSupportFragmentManager(), foodDetailsFragment, R.id.contentFrame, "FoodDetails");

can some help me to tell how to properly handle orientation change in case of 1 Activity and multiple Fragments?

I have almost tried all the solutions on StackOverflow and other places and nothing seem to work.

Answer 1

Please follow android basic documentation. I hope your doubt will be cleared.

https://developer.android.com/guide/topics/resources/runtime-changes.html

Rent Charter Buses Company
READ ALSO
Can you put multiple extra_text's in an android emailIntent

Can you put multiple extra_text's in an android emailIntent

Is it possible to put multiple multiple EXTRA_TEXT's

262
Could not create parent directory for lock file

Could not create parent directory for lock file

Error: Failed to complete Gradle execution

1129
Multi-threading with different Buttons view: Android

Multi-threading with different Buttons view: Android

So I am writing an android code where there are 3 buttons:record, pause, take frameRecord button records a video, pause button pauses the video displaying in VideoView and Take frame button shows the paused frame in imageView

290
App debugging showing non existing image path in the java code

App debugging showing non existing image path in the java code

When debugging my app, it's currently returning a jpg file that I'm not able to see in my adb

261