How to Save Fragment State in Fragment

Oct 6 2017 1:19 PM
MainActivity
 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.astuetz.PagerSlidingTabStrip;

public class MainActivity extends AppCompatActivity {

Fragment f = new Fragment();
LoginFragment f1=new LoginFragment();
SignupFragment f2=new SignupFragment();
public static PagerSlidingTabStrip tabs;
public static ViewPager pager;
private MyPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.viewpager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabs.setViewPager(pager);
}

public class MyPagerAdapter extends FragmentPagerAdapter {

private final String[] TITLES = {"Login","SignUp"};

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}

@Override
public int getCount() {
return TITLES.length;
}

@SuppressWarnings("unused")
private Fragment frag(Fragment f) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.viewpager, f);
transaction.commit();
return f;
}
@Override
public Fragment getItem(int position) {
if(position==0)
f = f1;//Home
else if(position==1)
f = f2;//DesiIndian
return f;
}

}
}
LoginActivity
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


/**
* A simple {@link Fragment} subclass.
*/
public class LoginFragment extends Fragment
{
Button co;
TextView tv_count;
int count=0;
private Bundle savedState = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_login, container, false);
tv_count=(TextView)view.findViewById(R.id.tv_count);
co=(Button)view.findViewById(R.id.button);

co.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
tv_count.setText(""+count);
}
});
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState!=null) {
count = savedInstanceState.getInt("someVarA");
tv_count.setText("" + count);
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("someVarA", count);
}
}
SignupFragment
 
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


/**
* A simple {@link Fragment} subclass.
*/
public class LoginFragment extends Fragment
{
Button co;
TextView tv_count;
int count=0;
private Bundle savedState = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_login, container, false);
tv_count=(TextView)view.findViewById(R.id.tv_count);
co=(Button)view.findViewById(R.id.button);

co.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
tv_count.setText(""+count);
}
});
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState!=null) {
count = savedInstanceState.getInt("someVarA");
tv_count.setText("" + count);
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("someVarA", count);
}
}
activity_main
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="true"
android:layout_width="match_parent"
android:layout_height="48dp" />

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" />

</LinearLayout>
fragment_login
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.social.chaudhary.fragmentstatesaved.LoginFragment">

<Button
android:id="@+id/button"
android:text="Count"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/tv_count"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Count" />

</LinearLayout>
signup_fragment
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.social.chaudhary.fragmentstatesaved.SignupFragment">

<EditText
android:id="@+id/et"
android:hint="User"
android:focusable="true"
android:inputType="text"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
gradel
 
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.social.chaudhary.fragmentstatesaved"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
}
 
 
 

Answers (1)