Android Fragment Communication onSaveInstanceState

Reading Time: 2 minutes

In this tutorial we cover android fragment communication onSaveInstanceState. Point is to save counted number in the fragment and reuse it again when mobile device is rotated. Starting from previous example, add in “Upper” fragment onSaveInstanceState method.


Now, add more code with value-key pair. Key is string, and value in this example is int.
Further code change in “Upper” fragment is to create onCreate method.


public void onCreate(Bundle savedInstanceState){


count = 0;




count = savedInstanceState.getInt(“counter”,0);




We need to make small changes in “Lower” fragment as well. Declare

public class Lower extends Fragment {

TextView text;

String takenData;

Then check if saved instance state is null and if is not fill takenData with previous value and declare Text View, and set its text with saved data.


You can download Android Fragment Communication onSaveInstanceState

External links:

Android Fragment Communication onSaveInstanceState on Youtube
Android Fragment Communication onSaveInstanceState on Stackoverflow


Tagged with:

Leave a Reply

Your email address will not be published. Required fields are marked *