H. Tiwari
H. Tiwari

Reputation: 93

Show popup activity only once the app starts

I have added a popup activity inside my app which is popping after 15 seconds of my app start. But when I am opening another activity and coming back to my main activity the popup showing again. I want it to appear only the first time when user is opening the app. What changes I should make? Appreciate the help.

Here is the popup code:

    Handler handler = new Handler();

    handler.postDelayed(new Runnable() {

        @Override

        public void run() {


       if (context != null) {

                AlertDialog.Builder alert = new AlertDialog.Builder(context, R.style.MyAlertDialogStyle)
                        .setTitle("Title")
                        .setMessage("Message")
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // continue with delete
                                Intent intent = new Intent(MainActivity.this, WebActivity.class);
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialogInterface, int which) {
                                dialogInterface.dismiss();
                                // do nothing
                            }
                        });

                mDialog = alert.create();
                mDialog.getWindow().getAttributes().windowAnimations = R.style.MyAlertDialogStyle;
                if (!((Activity) context).isFinishing())
                mDialog.show();
                // .setIcon(R.drawable.inr1)
                // .show();

            }
        }
    }, 15000);

Upvotes: 0

Views: 3842

Answers (7)

sheriff oladejo
sheriff oladejo

Reputation: 1

Type finish(); after your startActivity(intent);

Upvotes: 0

Shahid Ghafoor
Shahid Ghafoor

Reputation: 1059

save a boolean inside sharedpreferences read its value and determine weather running for the first time or not.

here is the code that will help you.

public void firstimeRun(boolean value) {
    SharedPreferences sharedPreferences = getPrefrenceManager();
    sharedPreferences.edit().putBoolean("key", value).apply();
}


public boolean isRunningForthefirstTime() {
    SharedPreferences sharedPreferences = getPrefrenceManager();
    return sharedPreferences.getBoolean("key", false);
}


private SharedPreferences getPrefrenceManager() {
    return PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
}

Upvotes: 0

ManiTeja
ManiTeja

Reputation: 847

please check the activity lifecycle.

Note:Remove Handler. Note 2:Create method and call the dialog Note 3:check the below method in lifecycle.

onPause (): Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

Upvotes: 0

mgcaguioa
mgcaguioa

Reputation: 1493

Add this code in your MainActivity:

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

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    if(sharedPreferences.getBoolean("IS_FIRST_TIME", true)) {
        //show your dialog here
        //...
        //change the value of your sharedPreferences
        sharedPreferences.edit().putBoolean("IS_FIRST_TIME", false).apply();
    }
}

Upvotes: 0

MaxExplode
MaxExplode

Reputation: 2007

You can use a global variable in the application class like this.

public class global extends Application

// some Boolean variable to hold status

And make sure you put this class inside android manifests application tag

android:name

Upvotes: 0

Gjson
Gjson

Reputation: 99

save a tag(counter) in sharedPrefrances for that When the application starts up interpretation is the first time app start

Upvotes: 0

Bhushan
Bhushan

Reputation: 424

You can do this if it needs to pop up only on application start

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

         //Set some flag on global constant

    }
}

Upvotes: 2

Related Questions