On-boarding slider screen - android

I have used a built in library to create "android on-boarding slider screen". The library is implementation 'com.github.apl-devs:appintro:v4.2.3'. The intro screen should open only first time when the app is launched but it opens everytime i run my app. How to launch is only the first time?
public class IntroActivity extends AppIntro {
private PrefManager prefManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addSlide(AppIntroFragment.newInstance("First","This is the first page",
R.drawable.sugar, ContextCompat.getColor(getApplicationContext(),R.color.colorAccent)));
addSlide(AppIntroFragment.newInstance("Second","This is the second page",
R.drawable.baseline_card_giftcard_black_24dp, ContextCompat.getColor(getApplicationContext(),R.color.colorPrimary)));
addSlide(AppIntroFragment.newInstance("Third","This is the third page",
R.drawable.baseline_fastfood_black_18dp, ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark)));
}
#Override
public void onDonePressed(Fragment currentFragment) {
super.onDonePressed(currentFragment);
Intent intent = new Intent(IntroActivity.this,MainActivity.class);
startActivity(intent);
}
#Override
public void onSkipPressed(Fragment currentFragment) {
super.onSkipPressed(currentFragment);
Intent intent = new Intent(IntroActivity.this,MainActivity.class);
startActivity(intent);
}
}

From the documentation for the library, available here:
Finally, declare the activity in your Manifest like so:
<activity android:name="com.example.example.intro"
android:label="#string/app_intro" />
Do not declare the intro as your main app launcher unless you want the intro to launch every time your app starts. Refer to the wiki for an example of how to launch the intro once from your main activity.
This is what the Wiki is referring to:
If the above method is unclear or you're not able to implement the same, then try writing the following code which uses SharedPreferences in your MainActivity.java file:-
/* In your onCreate method */
SharedPreferences sp = getSharedPreferences(MyPrefs, Context.MODE_PRIVATE);
if (!sp.getBoolean("first", false)) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("first", true);
editor.apply();
Intent intent = new Intent(this, IntroActivity.class); // Call the AppIntro java class
startActivity(intent);
}
This code reads a shared preference, and if it is found to not exist, or if it's value is false, it creates or edits the preference (so that the condition fails next time) then opens the intro screen.

Related

Shared preferences not working as expected

Below is my code to the MainActivity.java of my project, this is the welcome like screen for my app and should only appear first time user opens the app. Otherwise the user should get to see the Medicine_Activity.java which is also triggered when the user presses "Get Started" button on MainActivity. In order to implement this i came across something known as SharedPreferences and tried to implement it. But it isnt working quite as expected, the MainActivity flashes for a second before Medicine_Activity is launched. I am new so please help me out
Here is a video clip to view this bug in action - https://www.dropbox.com/s/fqbo9urb6xnh3pd/WhatsApp%20Video%202019-06-20%20at%202.12.50%20PM.mp4?dl=0
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnGetStarted;
#Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
if(!previouslyStarted) {
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
edit.apply();
} else{
Intent intent = new Intent(this, Medicine_Activity.class);
startActivity(intent);
}
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.activity_main);
Button btnGetStarted = findViewById(R.id.btnGetStarted);
btnGetStarted.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this, Medicine_Activity.class);
startActivity(intent);
}
}
Use commit() while storing the true and finish() the MainActivity.
if(!previouslyStarted) {
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(getString(R.string.pref_previously_started), true);
edit.commit();
} else{
Intent intent = new Intent(this, Medicine_Activity.class);
startActivity(intent);
finish();
}
A short explanation: commit() writes the data synchronously (blocking the thread its called from). It then informs you about the success of the operation. However, apply() schedules the data to be written asynchronously. It does not inform you about the success of the operation.
Quoting from Documentation:
Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures

Android Studio: Repoen App with Previous Screen

I'm writing an app with 8 different Activities that are all interconnected(accessible from one another). I'm wondering how I can have the app always reopen from a full close (not just home button click but from closing the background activity) with the same screen it was exited from.
For example, from my Splash Screen I navigate to a home screen. From that home screen I navigate to the Settings. Now, I click the multi-tab viewer and close all apps. How can I have Settings(or wherever I had left off) become the launching activity when the app is reopened?
Thanks in advance!
You can use SharedPreferences to store the class name of the latest Activity. Later, when relaunch, you can redirect to it from the Splash Screen.
You can do that simply by using SharedPreferences. When you launch an activity, it changes it's last activity value in SharedPreferences to point at that activity.
When you close the app and then reopen it, your Main/Launcher activity (Which may be your Splash Screen) then checks for this value and launches the activity according to this value which represents the last activity the app was on.
Here is the code that would fit the Main/Launcher activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String lastActivity = getDefaults("LAST_ACTIVITY", MainActivity.this);
Intent intentLastActivity;
// use an if statement to find out which activity it is
if(!lastActivity.equals("") || !lastActivity.equals(null)){
if(lastActivity.equals("home")){
intentLastActivity = new Intent(MainActivity.this, HomeActivity.class);
}else if(lastActivity.equals("settings")){
intentLastActivity = new Intent(MainActivity.this, SettingsActivity.class);
// add more else-if statements for the other activities.
}
startActivity(intentLastActivity); // launch the last activity
finish(); // finish/close the current activity.
}
}
public static void setDefaults(String key, String value, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
Once that code is placed in your Main/Launcher activity. You can then set this last activity value in SharedPreferences in other activities when they are launched.
Code placed in the other activities (in the onCreate method):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// we are at the settings activity. Change last activity value to settings
MainActivity.setDefaults("LAST_ACTIVITY","settings",this);
}
Luckily the methods to change the last activity are accessible in other activities.
If you're able to implement this well in your app. It should work as you want it to.
HOPE THIS HELPS!

App resumes to wrong activity when singleTask flag is used

MainActivity in my app has launchMode set to singleTask. If I start ActivityB from MainActivity, then put app to background and start my app from applications screen it does not resume correctly. ActivityB automatically finishes and MainActivity resumes. I expect ActivityB to resume instead. Why is this happening and what could I do to make it work normally? It works OK without singleTask flag but I need that flag for other purposes.
By the way, my app resumes correctly from recent apps screen.
Use the following code in the LAUNCHER Activities.
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isTaskRoot()) {
finish();
return;
}
// Rest of your onCreate code goes here
}
Activity B is closed because it launched from Activity A, this is normal behavior for android because Activity A needs to be restarted (singleTask) and all related instance will be kill/finish.
So, what you can do is implement shared-preference.
your activity A should be like this :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//// read share preference
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger("MYSHARE_PRFERENCE");
int isOpened = sharedPref.getInt("IS_ACTIVITY_B_ALREADY_OPENED", defaultValue);
if (isOpened == 1)
{
//resume activity B
startActivity(new Intent(MainActivity.this, ActivityB.class));
}
////
findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, ActivityB.class));
//// write on share preference
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("IS_ACTIVITY_B_ALREADY_OPENED", 1);
editor.commit();
////
}
});
}
}
P.S : I didn't compile it yet, the code might be error on compile. But share preference can be solution for your problem.

Android:Splash screen only for the first time

I need to create a splash screen that should displays only at the first time when an application is installed and launched and from the second time the when the app is launched it should shows the main activity?How can i achieve it ?
Okey looking at your problem you can do following..
First of all declare object of SharedPreference and on String which will we use later.
SharedPreferences loginPreference;
String MY_PREF = "my_pref";
Now in onCreate of your SplashActivity, do something like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize SharePreference
loginPreference = getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);
// this condition will do the trick.
if(loginPreference.getString("tag", "notok").equals("notok")){
// add tag in SharedPreference here..
Editor edit = loginPreference.edit();
edit.putString("tag", "ok");
edit.commit();
// your logic of splash will go here.
setContentView(R.layout.splash);
}else if(loginPreference.getString("tag", null).equals("ok")){
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}
Happy Coding..

Showing the setup screen only on first launch in android

I am making an android application but i can't figure out how i can make the setup screen show up only the first time.
This is how the application is going to work:
User launches the application after installation and is being shown the welcome/setup screen. And once the user is done with the setup, the setup screens will never appear again unless the user reinstalls the application.
How can i make this happen???
Please help and thanks SO much in advance!
Use SharedPreferences to test whether its the first start or not.
Note: The below code was not tested.
In your onCreate (or whereever you want to do things depending on first start or not), add
// here goes standard code
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
if(pref.getBoolean("firststart", true)){
// update sharedpreference - another start wont be the first
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("firststart", false);
editor.commit(); // apply changes
// first start, show your dialog | first-run code goes here
}
// here goes standard code
Make one helper activity. This will be your launcher activity.It will not contain any layouts, It will just check for first fresh run of an app. If It will first run, then setup activity will be started otherwise MainActivity will be start.
public class HelperActivity extends Activity {
SharedPreferences prefs = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
#Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
//strat DataActivity beacuase its your app first run
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
startActivity(new Intent(HelperActivity.ths , SetupActivity.class));
finish();
}
else {
startActivity(new Intent(HelperActivity.ths , MainActivity.class));
finish();
}
}
}

Categories

Resources