Start activity only once

落爺英雄遲暮 提交于 2019-12-01 08:56:43

When the app launches, set a flag in the activity preferences that the activity has already run. Default your setting to false, and then only start that activity if the flag isn't set. Note that if the user cleans your application data, or uninstalls it and later installs it again the activity would show again.

I think that you talk about activities like "log" page that you would launch only once in your application's whole life. For this, you can use sharedPreferences:

SharedPreferences prefs;
SharedPreferences.Editor editor;

and below your startActivity(intent), you add:

prefs = getSharedPreferences("nbRepet",Context.MODE_PRIVATE);
editor = prefs.edit();
editor.putInt("nbRepet", 1);
editor.commit();

Now your variable nbRepet have 1 as value.

After that, you can add these lines ABOVE your startActivity(intent) to verify that your activity was never launched before:

//here you recover nbRepet's value..

preferences = MainActivity.this.getSharedPreferences("nbRepet", MODE_PRIVATE);      
int value = preferences.getInt("nbRepet", 0);

//.. and you verify if your activity is launched before.

if(value<1)
{
    startActivity(intent);
}

You will need an Activity which checks a persisted boolean. ie,

onCreate(Bundle bundle)
{
    boolean firstRun = // persistance method here
    Intent toForward;
    if(firstRun)
        // Create an intent to start you "only once" activity
        // persist "firstRun" as false;
    else
        // Create an intent for your usual startup
    startActivity(toForward);
    finish();
}

this will do the job for you

package com.example.startup;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main); //we don't need this line
    SharedPreferences settings=getSharedPreferences("prefs",0);
    boolean firstRun=settings.getBoolean("firstRun",false);
    if(firstRun==false)//if running for first time
      {
        SharedPreferences.Editor editor=settings.edit();
        editor.putBoolean("firstRun",true);
        editor.commit();
        Intent i=new Intent(MainActivity.this,Second.class);//Activity to be     launched For the First time 
        startActivity(i);
        finish();
    }
    else
    {
        Intent a=new Intent(MainActivity.this,Main.class);//Default Activity 
        startActivity(a);
        finish();
    }
   }

}

Depends on what "first time" means. Easiest way is to put some flag into the SharedPreferences... but that can stay around for a while. ^^

If you mean that onCreate should be invoked only once when "start only once when app is launched", you could set the launchMode of the acitivty to singleInstance or singleTask in the manifest.

Devin

See my answer in this post, regarding this same issue.

I recommend checking the SharedPreference value for whether the app has previously been ran, inside of the onResume().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!