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");
}
Related
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);
I open a selection fragment using androidx.navigation, but I cannot find a way to return data to the previous fragment.
In one parent activity.. just use global variable that able to access inside same parent activity or
Other trick you can try preference, so that preference will able to read anywhere you want.
Data share active fragment
sharedPref = context.getSharedPreferences(context.getPackageName(),MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString(sKey, sValue);
prefEditor.apply();
Data read from other fragment
sharedPref = context.getSharedPreferences(context.getPackageName(),MODE_PRIVATE);
sValue = sharedPref.getString(sKey,defValue);
in my android app there is terms and condition fragment and in that there are two buttons accept and decline, the scenario is when user click on terms and condition the fragment open and user have two options accept and declined and when he press the accept button it return to main activity and there is one checkbox that should be check when user click on accept button in the fragment,So can anyone tell me how should i do that?
By using SharedPreferences short quick example:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putBoolean("switchOn", true);
editor.commit();
on the second activity
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
boolean buttonOn = editor.getBoolean("switchOn", false);
I have Activity A that has String that updates every 1s.
I also have Fragment B which has a Button and a TextView.
When I press my button in Fragment B I want to get String from Activity A to my TextView.
How do I do this?
SharedPreferences.Editor editor = getSharedPreferences(YOUR_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("DATA_NAME", "YOUR_DATA");
editor.commit();
On the Button Click
SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_NAME, MODE_PRIVATE);
String restoredData = prefs.getString("DATA_NAME", DEFAULT);
yourTextView.setText(restoredData );
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();