There are a lot of ways to do this. One of the simple way is using SharedPreferences. SharedPreferences will save the last index of tab that opened. When user open the activity when the sliding tab was placed, sharedPreference will retrieve the index that stored, then that index will matched with index of tab's item.
I hope you understand the explanation above, sorry for a worst english that i use. hahaha.
Okay for the detail, let's start the coding ....
I using THIS PROJECT to implementing how to open specific tab. But you can straight do in your project.
What should you do in your Fragment or on page where you start open the tab?
In every fragment, you have to store its index on sharedPreferences. Because later, that index will be leads you to open specific tab/fragment.
For example do this on your Fragment :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.androidbie.slidingtabsupportdesign.fragments; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import com.androidbie.slidingtabsupportdesign.MainActivity; | |
import com.androidbie.slidingtabsupportdesign.R; | |
/** | |
* Created by putuguna on 13/01/17. | |
*/ | |
public class ThirdFragment extends Fragment { | |
private Button btnOpenCurrentFragment; | |
private SharedPreferences sharedPreferences; | |
private SharedPreferences.Editor editor; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_third, container, false); | |
} | |
@Override | |
public void onViewCreated(View view, Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
btnOpenCurrentFragment = (Button) view.findViewById(R.id.btn_open_current_fragment); | |
sharedPreferences = getActivity().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); | |
editor = sharedPreferences.edit(); | |
btnOpenCurrentFragment.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
editor.putString("tab_opened", "2"); | |
editor.commit(); | |
getActivity().startActivity(new Intent(getActivity(), MainActivity.class)); | |
getActivity().finish(); | |
} | |
}); | |
} | |
} |
What should you do in your Activity or on page where you hosting you sliding tab?
The activity that i mean is page where you hosting your sliding tab. Inside your onCreate() do like the following code :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//get current tab | |
sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE); | |
String position = sharedPreferences.getString("tab_opened", null); | |
if(position==null){ | |
viewPager.setCurrentItem(1,true); | |
}else if(position=="0"){ | |
viewPager.setCurrentItem(0,true); | |
}else if(position=="1"){ | |
viewPager.setCurrentItem(1,true); | |
}else if(position=="2"){ | |
viewPager.setCurrentItem(2,true); | |
} | |
}// <<<~~~ THAT IS END OF onCreate() method. |
If SharedPreference contains null, the application will open the default tab. But if SharedPreference contains index, then the tab will opening according with the index.
What should you do after the application closed (user close the application) or if the page where you hosting the sliding tab is leaved?
It depend to you application requirements. If your apps need open the last tab that opened by user, then you don't need to do anything anymore, because the code above will open the last tab that opened by user.
But, if your apps required to open the default tab after user close the application, then you have to add a little bit code in your onDestroy() method :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.putString("tab_opened", "1"); | |
editor.commit(); | |
} |
EmoticonEmoticon