user6066040
user6066040

Reputation:

how to pop up a dialog box for the first time when i launched my app for the first time?

i want to pop up a dialog box when my app is open for the first time in a device.i want to show in popup box that how to use the app. if the app is opening for the first time it will show the dialog box ,otherwise it will simply avoid to show the dialog box.and the activities also will change according to the first use or normal use.for first time use it will show a activity1 or else it will show activity2.please help me. this is my activity which show a image when app is opened

 @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_name);


new Timer().schedule(new TimerTask() {
    public void run() {
        // here i want to go  to another activity acording to the first time use or normal time

    }
}, 3000);
}

Upvotes: 1

Views: 1181

Answers (2)

Terry W
Terry W

Reputation: 203

final String FIRST_TIME_KEY = "com.example.app.MainActivity.firstTimeKey";
SharedPreferences sp =     PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstTime = sp.getBoolean(FIRST_TIME_KEY, false);
if(isFirstTime) {
    SharedPreferences.Editor edit = sp.edit();
    edit.putBoolean(FIRST_TIME_KEY, true);
    edit.apply();

//show the dialog
} 

Upvotes: 0

snehasish
snehasish

Reputation: 171

 public class class_name extends AppCompatActivity {
 public static final String MyPREFERENCES2 = "MyPrefs" ;
 SharedPreferences sharedpreferences2;
 public boolean isFirstRun;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_name);


new Timer().schedule(new TimerTask() {
    public void run() {
        checkFirstRun();

    }
}, 3000);
}


public void checkFirstRun() {
System.out.println("its in check first run");
isFirstRun = getSharedPreferences("PREFERENCE2",  MODE_PRIVATE).getBoolean("isFirstRun", true);
if (isFirstRun){
   startActivity(new Intent(class_name.this, new_activity1.class));

    getSharedPreferences("PREFERENCE2", MODE_PRIVATE)
            .edit()
            .putBoolean("isFirstRun", false)
            .commit();

}
else{
     startActivity(new Intent(class_name.this, new_activity2.class));

    }
 }
}

Upvotes: 1

Related Questions