Question

I'm using this code to create a popup dialog in my home activity to ask user to rate my app, the codes work fine, however when users choose the option "No Thanks", the dialog should not show again.

However when the app is reopened, my homeActivity reloads, everything in it is reset, so despite this

editor.putBoolean("dontshowagain", true);
editor.commit();

the dialog will show again, is there anyway I can store the boolean value when the activity is reloaded?

public static class AppRater {

    private final int DAYS_UNTIL_PROMPT = 3;
    private final int LAUNCHES_UNTIL_PROMPT = 7;

    public void app_launched(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
        if (prefs.getBoolean("dontshowagain", false)) { return ; }

        SharedPreferences.Editor editor = prefs.edit();

        // Increment launch counter
        long launch_count = prefs.getLong("launch_count", 0) + 1;
        editor.putLong("launch_count", launch_count);

        // Get date of first launch
        Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
        if (date_firstLaunch == 0) {
            date_firstLaunch = System.currentTimeMillis();
            editor.putLong("date_firstlaunch", date_firstLaunch);
        }

                    // i just use this to test the dialog instantly
            showRateDialog(mContext, editor);



        // Wait at least n days before opening
        if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
            if (System.currentTimeMillis() >= date_firstLaunch + 
                    (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
                showRateDialog(mContext, editor);
            }
        }

        editor.commit();
    }   

    public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
        final Dialog dialog = new Dialog(mContext);
        dialog.setTitle("Rate MyApp");

        LinearLayout ll = new LinearLayout(mContext);
        ll.setOrientation(LinearLayout.VERTICAL);

        TextView tv = new TextView(mContext);
        tv.setText("We see that you have been using MyApp well. Would you like to rate us?");
        tv.setWidth(240);
        tv.setPadding(4, 0, 4, 10);
        ll.addView(tv);

        Button b1 = new Button(mContext);
        b1.setText("Rate MyApp");
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Uri uri = Uri.parse("market://details?id=" + mContext.getPackageName());
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                try {
                  mContext.startActivity(goToMarket); // playstore installed
                } catch (ActivityNotFoundException e) { // open website if not
                    mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + mContext.getPackageName())));
                }
                dialog.dismiss();
            }
        });        
        ll.addView(b1);

        Button b2 = new Button(mContext);
        b2.setText("Remind me later");
        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ll.addView(b2);


        Button b3 = new Button(mContext);
        b3.setText("No, thanks");
        b3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                rated = true;
                if (editor != null) {
                    editor.putBoolean("dontshowagain", true);
                    editor.commit();
                }
                dialog.dismiss();
            }
        });
        ll.addView(b3);

        dialog.setContentView(ll);        
        dialog.show();    
    }
}
// http://www.androidsnippets.com/prompt-engaged-users-to-rate-your-app-in-the-android-market-appirater
Was it helpful?

Solution

Nice problem. I think the reason is, that you commit the editor at the end of method app_launched, and the button press in the dialog happens some time later, so when you do this editor.putBoolean("dontshowagain", true), the editor is already commited and thus your entry is not saved in the preferences.

If you need help on how to change your code to fix this, let me know in a comment.

EDIT - Some code

First, do not pass the editor to your showRateDialog method, so change method signature to:

public static void showRateDialog(final Context mContext)

Second, in your onClick method, create a new Editor, write the flag and commit.

public void onClick(View v) {
    rated = true;
    SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
    // create editor, write stuff and commit, all in one line.
    prefs.edit().putBoolean("dontshowagain", true).commit();
    dialog.dismiss();
}

OTHER TIPS

Ridicully is correct. Use this code instead and open a new editor:

b3.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        rated = true;
        SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
        SharedPreferences.Editor editorNew = prefs.edit();
        editorNew.putBoolean("dontshowagain", true);
        editorNew.commit();

        dialog.dismiss();
    }
});

Then clean up the showRateDialog by removing the editor parameter. Remember that the code of the listeners are called long after the methods setting them up has returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top