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..
Related
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
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.
I have an app developed on android studio, and when you start its showing a SplashScreen with the icon an a loading bar, and when its done, the app auto start a class called Slider and have 4 sliders for the new users to know how to use the app and then, the user click on a button and redirect to MainActivity, ok, so i want, if a users is old, to don't display the Slider class and redirect automatically to MainActivity, I'm saying, to display the slider only for new installations, if anyone can help me.. y tried so many hours an i don't get nothing works... Thanks to all!
Only for new installation.
Use SharedPreferences and store your first installation flag to run your Settings at first time.
Like,
private SharedPreferences prefs = null;
private void isFirstTime()
{
prefs = getSharedPreferences("appPref", MODE_PRIVATE);
if (prefs.getBoolean("firstInstall", true)) {
// Your setting activity start here
prefs.edit().putBoolean("firstInstall", false).commit();
}
}
Hi maybe i understand your problem, maybe one of more solution:
SharedPreferences prefs = context.getSharedPreferences("com.example.app", context.MODE_PRIVATE);
//check if the first run of app
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
//for example i run another class if is the first run
Intent intent = new Intent(context, MyClass.class);
context.startActivity(intent);
}
if Slider is the Launcher the in your Slider.class use Shared Preference,just add this lines as first lines in your public void init(Bundle bundle) method
SharedPreference pref;
#Override
public void init(Bundle bundle) {
pref =SharedPreference.getSharedPrefernce("APP",Context.MODE_PRIVATE);
if(pref.getBoolean("first",false)) // false for first time so it wont start Mainactivity without slider
{
SharedPreference.Editor editor= pref.edit();
editor.putBoolean(true);// from second time it will be false
editor.commit();
Intent in=new Intent(Slider.this,MainActivity.class);
startActivity(in);
}
//Existing Slider Code
}
FINALLY I SOLVED THIS!! AFTER COUPLE HOURS.. thanks to everyone.
Finally i put on MainActivity in onCreate this:
SharedPreferences prefs = null;
{
prefs = getSharedPreferences("com.sorte.app", MODE_PRIVATE);
if (prefs.getBoolean("firstInstall", true)) {
Intent i = new Intent(MainActivity.this, Slide.class);
startActivity(i);
prefs.edit().putBoolean("firstInstall", false).commit();
}
}
i want to display a layout that takes user information and save them.
Then, when the user open the application for second time it shows different layout.
How i can do that??
and Please if you can help by just mentioning the right keywords so i can also search by myself I will be great-full
Use this in your onCreate() method:
#Override
protected void onCreate(Bundle bundle) {
boolean firstStart = getSharedPreferences("SomeName", MODE_PRIVATE).getBoolean("firstStart", true);
if(!firstStart){
Intent intent = new Intent(this, SomeOtherActivity.class);
startActivity(intent);
}
}
Make sure you write false to the shared prefferences after your first start:
SharedPreferences prefs = getSharedPreferences("SomeName", MODE_PRIVATE);
Editor edit = prefs.edit();
edit.putBoolean("firtStart", false);
edit.apply();
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();
}
}
}