How to retain news article on android rotation fragment

When an Android app is rotated, the fragment's state is not automatically retained. This is because the fragment's instance is recreated when the activity is recreated. To retain the state of a news article on an Android rotation fragment, you can use the following approaches:

  1. Use the setRetainInstance() method: You can call setRetainInstance(true) on the fragment to retain its instance across configuration changes, such as screen rotations. This method is available in Android 3.0 (API level 11) and later.

Example:

public class NewsArticleFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
}
  1. Use the onSaveInstanceState() and onActivityCreated() methods: You can override the onSaveInstanceState() method to save the fragment's state, and then restore it in the onActivityCreated() method.

Example:

public class NewsArticleFragment extends Fragment {
    private String articleText;

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

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("article_text", articleText);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState!= null) {
            articleText = savedInstanceState.getString("article_text");
        }
    }
}
  1. Use a ViewModel: You can use a ViewModel to store the fragment's state and retain it across configuration changes. A ViewModel is a class that holds UI-related data and is tied to a specific lifecycle of an activity or fragment.

Example:

public class NewsArticleViewModel extends ViewModel {
    private String articleText;

    public String getArticleText() {
        return articleText;
    }

    public void setArticleText(String articleText) {
        this.articleText = articleText;
    }
}

public class NewsArticleFragment extends Fragment {
    private NewsArticleViewModel viewModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = ViewModelProviders.of(this).get(NewsArticleViewModel.class);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel.getArticleText().observe(getViewLifecycleOwner(), articleText -> {
            // Update the UI with the article text
        });
    }
}
  1. Use a Bundle: You can pass the fragment's state as a Bundle to the fragment's constructor and then retrieve it in the onActivityCreated() method.

Example:

public class NewsArticleFragment extends Fragment {
    private String articleText;

    public NewsArticleFragment(Bundle bundle) {
        articleText = bundle.getString("article_text");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Update the UI with the article text
    }
}

In your activity, you would create the fragment with the Bundle:

NewsArticleFragment fragment = new NewsArticleFragment(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit();

Remember to handle the orientation change in your activity's onConfigurationChanged() method:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Update the UI as needed
}

By using one of these approaches, you can retain the state of your news article fragment across Android screen rotations.