This question already has answers here:
Android application restarts on orientation change
(6 answers)
Closed 2 years ago.
My app contains fragments, and it's mainly a fragment application. I have only 3 activities, others are all fragments. But from any point of the app when I rotate the phone in landscape mode or portrait the application restarts and continues from the Start Point of the app, from the beginning.
I put the android:configChanges="orientation|keyboardHidden|keyboard" in the Manifest, but it seems it has no effect at all. I'm populating the fragments only with one web view. But I also change the actionbar color. That's all I do in the app. It's a static app, there is no user input. Here's a sample code of how my fragments look like:
public class CLASSNAME extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.LAYOUT, container, false);
ActionBar bar = getActivity().getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.COLOR)));
bar.setTitle("TITLE");
final WebView wFirst = (WebView) view.findViewById(R.id.WEBVIEWID);
wFirst.getSettings().setJavaScriptEnabled(true);
wFirst.setBackgroundColor(0x00000000);
wFirst.loadDataWithBaseURL(LOADING DATA);
wFirst.setWebViewClient(new WebViewClient() {SETTING UP WEBVIEW CLIENT});
return view;
}
}
I read lots of posts that got answered with Save Instance State and those other methods, but in my case I have no idea what to save and how to keep my activity rolling even if the orientation is changed.
This is the MainActivity.class code:
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
private static CharSequence mTitle;
public static Context context;
public static Context getContext() {
return context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
switch (position) {
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, new AlgebraicGraphs())
.commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.container, new Logarithms())
.commit();
break;
case 3:
fragmentManager.beginTransaction()
.replace(R.id.container, new Polynomials())
.commit();
break;
case 4:
fragmentManager.beginTransaction()
.replace(R.id.container, new Powers())
.commit();
break;
case 6:
fragmentManager.beginTransaction()
.replace(R.id.container, new AreaFormulas())
.commit();
break;
case 7:
fragmentManager.beginTransaction()
.replace(R.id.container, new SurfaceAreaFormulas())
.commit();
break;
case 8:
fragmentManager.beginTransaction()
.replace(R.id.container, new PerimeterFormulas())
.commit();
break;
case 9:
fragmentManager.beginTransaction()
.replace(R.id.container, new VolumeFormulas())
.commit();
break;
case 11:
fragmentManager.beginTransaction()
.replace(R.id.container, new TrigonometryGraphsFormulas())
.commit();
break;
case 12:
fragmentManager.beginTransaction()
.replace(R.id.container, new HyperbolicIdentities())
.commit();
break;
case 13:
fragmentManager.beginTransaction()
.replace(R.id.container, new TrigonometricIdentities())
.commit();
break;
case 16:
fragmentManager.beginTransaction()
.replace(R.id.container, new IntegralIdentities())
.commit();
break;
case 17:
fragmentManager.beginTransaction()
.replace(R.id.container, new IntegralSpecialFunctions())
.commit();
break;
case 18:
fragmentManager.beginTransaction()
.replace(R.id.container, new TableOfIntegrals())
.commit();
break;
case 20:
fragmentManager.beginTransaction()
.replace(R.id.container, new DerivativeIdentities())
.commit();
break;
case 21:
fragmentManager.beginTransaction()
.replace(R.id.container, new TableOfDerivatives())
.commit();
break;
default:
fragmentManager.beginTransaction()
.replace(R.id.container, new DefaultLayout())
.commit();
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_about) {
About about = new About();
about.show(getFragmentManager(), "about");
}
return false;
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
This is the log:
02-19 06:41:24.849 9788-9788/l.pocketformulas E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Fragment Logarithms{4132f5d0} not attached to Activity
at android.app.Fragment.getResources(Fragment.java:828)
at l.pocketformulas.Logarithms$1.onPageFinished(Logarithms.java:71)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:444)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
In OnCreate() method of your main activity use this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Or you can also set Orientation in manifest.xml like:
<activity
android:name="com.example.fragment.AppMainTabActivity"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait"
android:theme="#style/AppTheme"
android:windowSoftInputMode="adjustPan" >
</activity>
Now your orientation will be set to portrait and will never change.
Update:
#Override
protected void onSaveInstanceState(Bundle outState) {
webView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}
Now, your onCreate() implement like:
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
if (savedInstanceState != null)
((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
}
These methods can be overridden on any activity, it just basically allows you to save and restore values each time an activity is created/destroyed, when the screen orientation changes the activity gets destroyed and recreated in the background, so therefore you could use these methods to temporary store/restore states during the change.
For an example go to this page :http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
I did solve this problem with a simple configuration on "AndroidManifest.xml":
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
have you checked this?
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Depending on your device you may need to include "screenSize" in confingChange. other wise your activity will restarted.
like this:
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="sensor" />
Related
I have 3 fragments, Home, Menu and orders they can be loaded by bottomnavigtioview items and the title shown by it as well
once navigate from Home to Orders then if you want to go back to Home From Orders the title still "Orders"
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.navigation_home:
toolbar.setTitle(getResources().getString(R.string.title_home));
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_menu:
toolbar.setTitle(getResources().getString(R.string.fragment_title_menu));
fragment = new ProductFragment();
loadFragment(fragment);
return true;
case R.id.navigation_orders:
toolbar.setTitle(getResources().getString(R.string.title_orders));
fragment = new OrdersFragment();
loadFragment(fragment);
return true;
}
return false;
}
};
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
// attaching bottom sheet behaviour - hide / show on scroll
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) navigation.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationBehavior());
toolbar.setTitle(getResources().getString(R.string.title_home));
Fragment f = HomeFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, f)
.commit();
}`
I have Also Tried this but its not working
`
fragmentmanger.addOnBackStackChangedListener -> public void onBackStackChanged() {
}`
Instead of calling toolbar.setTitle in your onNavigationItemSelected method, move it to onCreateView of each fragment. When you navigate back to a Fragment, onCreateView is executed again.
It worked for me when calling it in onResume like this in each fragment:
if(getActivity()!=null) {
((MainActivity)getActivity()).setActionBarTitle
(context.getResources().getString(R.string.YOUR_TITLE));
this is the function setActionBarTitle in MainActivity
public void setActionBarTitle(String title) {
toolbar.setTitle(title);
}
I need some help as I am a bit new in android, in my android app that I am working on, I am navigating between Fragments all the time. I use the code to navigate between Fragments.
private void displaySelectedScreen(int id){
Fragment fragment=null;
String fragment_tag="";
switch(id)
{
case R.id.nav_listen:
fragment=new ListenFragment();
fragment_tag="LISTENTAG";
break;
case R.id.nav_programs:
break;
case R.id.nav_stories:
break;
case R.id.nav_about:
break;
case R.id.nav_feedback:
break;
case R.id.nav_share:
shareTextUrl();
break;
}
if(fragment!=null){
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,fragment,fragment_tag);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
I just need to check that if the fragment exists(loaded) before, containter should show the previously loaded fragment. However, if the Fragment was not loaded before the Fragment should be loaded for the first time.
Could anyone help me about making this happen?
Thank you very much.
Code inside my fragment:(ListenFragment.java)
public class ListenFragment extends Fragment
{
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Listen Live");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View InputFragmentView=inflater.inflate(R.layout.listen_layout,container,false);
return InputFragmentView;
}
You don't need to declare the variables reference to Fragment's instance. Instead, save your fragment by tag then get it. Like that:
Fragment fragment = getSupportFragmentManager().findFragmentByTag("fragment_tag");
if(fragment == null) {
fragment=new ListenFragment();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment, fragment_tag);
ft.commit();
use below methods to get the Fragment:
getFragmentManager().findFragmentById(R.is.xyz)
getFragmentManager().findFragmentByTAG("XYZ")
then use to start Fragment :
supportFragmentManager.beginTransaction()
there is a similar question here it might help:
Check if a fragment exists and reuse it
I implemented a bottom Navigation that should be filled with three different Fragment classes.
My Problem is that if i start the activity the first Fragment layout is not shown, only if i go to tab2 and return to tab1.
I think it is a Problem with the LifeCycle but i have no idea how i can fix it.
my bottom Navigation class that handle the Navigation:
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new RequestFragment();
break;
case R.id.navigation_dashboard:
fragment = new ResponseFragment();
break;
case R.id.navigation_notifications:
fragment = new LogfileFragment();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
And my Fragment class:
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_request, container, false);
you should add the first fragment when activity start. you just set first fragment on onNavigationItemSelected() method and it never called even you click on navigation View. you should add these lines in onCreate() method:
fragment = new RequestFragment();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
You have to initialize the fragment inside onCreate() method.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
...
fragment = new RequestFragment();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
}
This will display the fragment in the container inside the view when your activity starts. Keep your onNavigationItemSelected method as it is.
I'm using HashMap of fragment's backstack. To save backstack and current fragment I use the code below:
public class MainActivity extends AppCompatActivity {
private HashMap<String, Stack<Fragment>> mStacks;
public static final String TAB_PROFILE = "tab_profile";
public static final String TAB_DASHBOARD = "tab_dashboard";
public static final String TAB_CHATS = "tab_chats";
public static final String TAB_SETTINGS = "tab_settings";
private String mCurrentTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
if (savedInstanceState != null) {
mCurrentTab = savedInstanceState.getString("currentTab");
mStacks = (HashMap<String, Stack<Fragment>>) savedInstanceState.getSerializable("stacks");
} else
selectedTab(TAB_DASHBOARD);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("stacks", mStacks);
outState.putString("currentTab", mCurrentTab);
}
private void setupViews() {
mStacks = new HashMap<>();
mStacks.put(TAB_PROFILE, new Stack<>());
mStacks.put(TAB_DASHBOARD, new Stack<>());
mStacks.put(TAB_CHATS, new Stack<>());
mStacks.put(TAB_SETTINGS, new Stack<>());
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.action_dashboard);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.action_profile:
selectedTab(TAB_PROFILE);
return true;
case R.id.action_dashboard:
selectedTab(TAB_DASHBOARD);
return true;
case R.id.action_chats:
selectedTab(TAB_CHATS);
return true;
case R.id.action_settings:
selectedTab(TAB_SETTINGS);
return true;
}
return true;
});
bottomNavigationView.setOnNavigationItemReselectedListener(item -> {
if (mStacks.get(mCurrentTab).size() != 1) {
mStacks.get(mCurrentTab).clear();
switch (item.getItemId()) {
case R.id.action_profile:
selectedTab(TAB_PROFILE);
break;
case R.id.action_dashboard:
selectedTab(TAB_DASHBOARD);
break;
case R.id.action_chats:
selectedTab(TAB_CHATS);
break;
case R.id.action_settings:
selectedTab(TAB_SETTINGS);
break;
}
}
});
}
private void selectedTab(String tabId) {
mCurrentTab = tabId;
if(mStacks.get(tabId).size() == 0){
if(tabId.equals(TAB_PROFILE)){
Fragment fragment = new ProfileFragment();
Bundle args = new Bundle();
args.putSerializable("user", Globals.getCurrentUser());
fragment.setArguments(args);
pushFragments(tabId, fragment,true);
} else if(tabId.equals(TAB_DASHBOARD)){
pushFragments(tabId, new DashboardFragment(),true);
}else if(tabId.equals(TAB_CHATS)){
pushFragments(tabId, new GroupsFragment(),true);
}else if(tabId.equals(TAB_SETTINGS)){
pushFragments(tabId, new SettingsFragment(),true);
}
}else {
pushFragments(tabId, mStacks.get(tabId).lastElement(),false);
}
}
public void pushFragments(String tag, Fragment fragment, boolean shouldAdd){
if(shouldAdd)
mStacks.get(tag).push(fragment);
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content, fragment);
ft.commit();
}
public void popFragments(){
Fragment fragment = mStacks.get(mCurrentTab).elementAt(mStacks.get(mCurrentTab).size() - 2);
mStacks.get(mCurrentTab).pop();
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content, fragment);
ft.commit();
}
#Override
public void onBackPressed() {
if(mStacks.get(mCurrentTab).size() == 1){
finish();
return;
}
popFragments();
}
}
Set new fragments using
((MainActivity)context).pushFragments(MainActivity.TAB_CHATS, fragment,true);
Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/background_material_light"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_navigation"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/waPrimary"
app:itemIconTint="#color/white"
app:itemTextColor="#color/white"
app:menu="#menu/menu_bottom_navigation" />
Everything works fine on screen rotation, but application crashes with exception on application hide.
java.lang.RuntimeException: Parcel: unable to marshal value %FragmentName%{c985244 #2 id=0x7f090051}
As I read, it happens when one of the objects I'm trying to pass is not Parceable, but have no idea how to fix this. Any thoughts?
UPD
After I made all of my fragments Serializable, new exception throws
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = %FragmentName%)
...
Caused by: java.io.NotSerializableException: android.support.v7.widget.RecyclerView
UPD2
Seems like a found a solution - transient property. Now I'm trying to make all non-serializeable objects transient.
UPD3
It helped, but I don't know is it efficient enough.
Here's my suggestion:
Your activity maintains a reference to the four fragments it wants for the bottom navigation toggling.
On toggling bottom navigation, you replace the current fragment in the activity fragment manager.
While on a given fragment, as you interact with the UI, you push things on to the fragment child fragment manager.
This way, each fragment maintains its own backstack automatically, you don't have to save any state, and it all Just Works™.
Some sample code that might help.
public class MainActivity extends AppCompatActivity {
private Fragment mProfileFragment;
private Fragment mDashboardFragment;
private Fragment mChatsFragment;
private Fragment mSettingsFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
// Init fragments
}
else {
// Find last active fragments in fragment manager
}
setupViews();
}
private void setupViews() {
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.action_dashboard);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
Fragment fragment;
switch (item.getItemId()) {
case R.id.action_profile:
fragment = mProfileFragment;
break;
case R.id.action_dashboard:
fragment = mDashboardFragment;
break;
case R.id.action_chats:
fragment = mChatsFragment;
break;
case R.id.action_settings:
fragment = mSettingsFragment;
break;
}
// Replace the currently active fragment which will be
// managing its own backstack
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frament_container, fragment)
.commit();
});
}
}
And one of your fragments would push stuff on its own stack like this:
public class ProfileFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.id.fragment_layout, container, false);
Button button = view.findViewById(R.id.some_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment someFragmentToPush = new SomeFragmentToPush();
// Use the child fragment manager to keep UI
// local to this fragment instance, adding to backstack
// for automatic popping on pressing back
getChildFragmentManager().beginTransaction()
.add(R.id.fragment_layout, someFragmentToPush)
.addToBackStack(null)
.commit();
}
});
return view;
}
}
Hope that helps!
I currently have a navigation drawer, which has a few fragments (Home, Help, About) in my activity. On startup it opens up Home. The issue i'm having is that when i go to another fragment such as Help and then proceed to put the phone to sleep and subsequently turn on the phone back on it'll always return to Home instead of help.
I'm quite new to lifecycles but was hoping to get some feedback on how to resume from a different fragment.
EDIT: Provided relevant code
Update: Realised that this happens because i reinit the views on resume.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUI();
}
private void initializeUI() {
fragAbout = new About();
fragHelp = new Help();
fragHome = new MyViewPager();
// Adding fragments to activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.main_activity_fraglayout, fragHome);
transaction.commit();
...
}
private void addDrawerItems() {
...
DrawerItemAdapter drawerAdapter = new DrawerItemAdapter(this, R.layout.nav_list_row, drawerItems);
mDrawerList.setAdapter(drawerAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
...
newFragOnClick(fragHome, "Home");
break;
case 1:
...
newFragOnClick(fragSettings, "Help");
break;
case 2:
...
newFragOnClick(fragAbout, "About");
break;
default:
break;
}
}
});
}
private void newFragOnClick(Fragment frag, String actionBarTitle){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_activity_fraglayout, frag);
transaction.commit();
}
Use sharedpreferences to save the current tab position and in onResume() use it to move to the saved position.