Show Fragment on First Launch - android

I'm trying to show a fragment only on the first time the application is launched. What I'm trying works to show the fragment but it always shows, I only want it to show on first launch.
I'm replacing fragments from a method called displayView, in a navigation drawer drawerlistener (this may be my problem), im trying to use a boolean and shared preferences in my method. Any ideas as to why this isn't working
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
firstTimeFrag();
}
}
private boolean firstTimeFrag(){
SharedPreferences pref = getPreferences(MODE_PRIVATE);
boolean ranBefore = pref.getBoolean("RanBefore",false);
if(!ranBefore){
SharedPreferences.Editor editor = pref.edit();
editor.commit();
Fragment fr = new ifFirstTimeFrag();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fr);
fragmentTransaction.commit();
}else{
displayView(0);
}
return ranBefore;
}

You are not saving anything ^^
SharedPreferences.Editor editor = pref.edit();
editor.commit();
Use this:
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("RanBefore", true);
editor.commit();

Related

How to save viewpager page position as bookmark?

I have MainActivity where i define viewpager and i also define button on toolbar.
I want when i click on button it save the viewpager page position with title in another activity in listview.
Provide complete example.
Try saving position of viepager in a shared preference.
save when going away from activity
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("viepager_position", postion);
editor.apply();
retrieve when user comes back to activity
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int position = prefs.getInt("viepager_position",default_position);

saving fragment transaction whereever

initially I have an activity A in which I'm gonna open one fragment so here how can i save that fragment
So, that when I launch my application after destroying it restores that fragment in that same Activity in same position
For, answering convenience here's my fragment transaction code:
Fragment newFragment = new ece_frag();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out);
transaction.replace(R.id.frame_layout, newFragment);
transaction.commit();
Lets say you have 3 fragments A,B and C.
I give index to each fragment like this say 0->A, 1->B, 2->C. So when i do this, I also save the index like the code below:
Fragment newFragment = new A();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out);
transaction.replace(R.id.frame_layout, newFragment);
transaction.commit();
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("last_fragment", 0);//For fragment A saving index 0
editor.commit();
And then in onCreate you can use an if case like this:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
//0 here is the default value
int lastFragment = sharedPref.getInt("last_fragment", 0);
Then you can do this
switch(lastFragment){
case 0:
//Load your fragment Here according to the index.
break;
case 1:
//Load your fragment Here according to the index.
break;
case 2:
//Load your fragment Here according to the index.
break;
}
Hope this helps.

Android Show once Box

I have see in many Apps a "FirstStart" View with hints from the App.
Titanium Backup shows at the first start a View with the Rules of the App and after this the Changelog.
Is this "FirstStart" a Addon or how can i make this ?
Not an addon. Just show a dialog using fragment manger and save a value to shared preferences.
Boolean isFirstRun = getActivity().getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE).getBoolean("firstRun", true);
if (isFirstRun) {
SharedPreferences sharedPreferences = getActivity().getApplicationContext().getSharedPreferences(SHARED_PREF, 0);
SharedPreferences.Editor editor= sharedPreferences.edit();
editor.putBoolean("firstRun", false);
editor.apply();
FragmentManager fm = getFragmentManager();
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(fm, "myDialog");
}

Proper way of saving Fragment transaction before exiting

I have my method for replacing Fragments in my mainActivity..like this after clicking my Button...
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
Fragment2 f2=new Fragment2();
trans.replace(R.id.root_frame, f2,"Fragment2");
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
trans.commit();
getPager().setCurrentItem(0);
and i need to save this change before exiting my app..so when i re-open my app transaction changes would be there...
You could save your fragment state in shared preferences and retrieve it when the fragment gets recreated.
Save your state when you set the transaction
SharedPreferences mypreferences=getActivity().getPreferences(MODE_PRIVATE);
Editor editor= mypreferences.edit();
editor.putString("transition","FragmentTransaction.TRANSIT_FRAGMENT_OPEN");
//do the same for other fragment states
editor.commit();
Retreive your state in your onCreate() method.
String transition= mypreferences.getString("transition", "");
if(!transition.equals(""){
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//do the same for other fragment states
}

Set textView text in navigation drawer fragment from another activity

This is my first time posting so bare with please.
I'm trying to set the text of a textView item in the navigation drawer fragment from another activity.
I've tried:
public class SomeActivity{
...
...
NavigationDrawerFragment.textViewName.setText("words");
}
public class SomeActivity{
...
...
NavigationDrawerFragment frag = (NavigationDrawerFragment )
getFragmentManager().findFragmentByTag("NavigationDrawerFragment");
frag.GetTextFromActivityMethod("words");
}
and I get a null pointer exception and class not loaded (NavigationDrawerFragment) error in debugger.
Try SharedPreference, here is a sample for your case:
public class SomeActivity{
NavigationDrawerFragment.textViewName.setText("words");
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("WORDS", b);
editor.commit();
}
public class SomeActivity {
NavigationDrawerFragment frag = (NavigationDrawerFragment)
getFragmentManager().findFragmentByTag("NavigationDrawerFragment");
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
String words = pref.getString("WORDS", null);
textView.setText
}
From main activity you can set the fragment drawer's text view value. I have set it and it works well!
In navigation Drawer Bar
public TextView test_text;
test_text = (TextView) viewtype.findviewbyid(R.id.xyz);
In the main activity
String ABC = preference.getString(key);
test_text.setText(ABC);

Categories

Resources