Open another activity when dialog's button is pressed?

Viewed 633

I'd like to successfully run the code below (belonging to this library) when any of the buttons from this Rating Dialog are pressed. How can I do it?

  public void goToPickerActivity() {
    Intent intent = new com.sucho.placepicker.PlacePicker.IntentBuilder()
            .onlyCoordinates(true) //Get only Coordinates from Place Picker
            .showLatLong(true)
            .setLatLong(40.71274, -74.005974)  // Initial Latitude and Longitude the Map will load into (NYC coordinates)
            .setMapZoom(2.5f)  // Map Zoom Level. Default: 14.0
            .build(this);

    startActivityForResult(intent, Constants.PLACE_PICKER_REQUEST);
  }

Edit: I want to execute goToPickerActivity() first and then execute the library's intended click code. For example, when Rate App button is clicked:

Step 1 is: execute goToPickerActivity().

Once that's complete, Step 2 is: run the usual Rate App clicked code.

6 Answers

You can try to add a OnRatingDialogListener using setRateButtonText, setRateLaterButtonText or setNeverRateButtonText methods of AppRatingDialog.Builder class:

final AppRatingDialog appRatingDialog = new AppRatingDialog.Builder(this)
            // ... call another setup methods if need
            .setRateButtonText("Rate App", new OnRatingDialogListener() {
                @Override
                public void onClick() {
                    goToPickerActivity();
                }
            })
            .setRateLaterButtonText("Rate App Later", new OnRatingDialogListener() {
                @Override
                public void onClick() {
                    goToPickerActivity();
                }
            })
            .setNeverRateButtonText("Never", new OnRatingDialogListener() {
                @Override
                public void onClick() {
                    goToPickerActivity();
                }
            })
            .build();

appRatingDialog.show();

Using lambda it will look like this:

final AppRatingDialog appRatingDialog = new AppRatingDialog.Builder(this)
        // ... call another setup methods if need
        .setRateButtonText("Rate App", () -> goToPickerActivity())
        .setRateLaterButtonText("Rate App Later", () -> goToPickerActivity())
        .setNeverRateButtonText("Never", () -> goToPickerActivity())
        .build();

appRatingDialog.show();

EDIT 1:

Method openPlayStore:

private void openPlayStore() {
    final String storeLink = "market://details?id=" + context.getPackageName();
    final Uri marketUri = Uri.parse(storeLink);
    try {
        mContext.startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(mContext, "Couldn't find PlayStore on this device", Toast.LENGTH_SHORT).show();
    }
}

If you can edit the library try to make openPlayStore method public, save reference to AppRatingDialog instanse to some variable, e.g. appRatingDialog and call appRatingDialog.openPlayStore().

EDIT 2:

To have different listeners for buttons we need to add new listeners to AppRatingDialog and handle them:

public class AppRatingDialog extends AppCompatDialog implements View.OnClickListener {
    //...
    private OnRatingDialogListener onRatingDialogListener;
    private OnRatingDialogListener onRateLaterDialogListener;
    private OnRatingDialogListener onRateNeverDialogListener;

    //...

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.dialog_rating_button_never_rate) {
            savedNeverShow();
            dismiss();

            if (onRateNeverDialogListener != null) {
                onRateNeverDialogListener.onClick();
            }
        } else if (v.getId() == R.id.dialog_rating_button_rate_later) {
            dismiss();
            incrementLaunchCount(true);

            if (onRateLaterDialogListener != null) {
                onRateLaterDialogListener.onClick();
            }
        } else if (v.getId() == R.id.dialog_rating_button_rate) {
            savedNeverShow();
            dismiss();

            if (onRatingDialogListener != null) {
                onRatingDialogListener.onClick();
            } else {
                openPlayStore();
            }
        }
    }

    // ...

    public static class Builder {
        //...

        @NonNull
        public Builder setRateLaterButtonText(String rateLaterButtonText, @Nullable OnRatingDialogListener onRateLaterClickListener) {
            appRatingDialog.mRateLaterButtonText = rateLaterButtonText;
            appRatingDialog.onRateLaterDialogListener = onRateLaterClickListener;
            return this;
        }

        @NonNull
        public Builder setNeverRateButtonText(String neverRateButtonText, @Nullable OnRatingDialogListener onNeverRateClickListener) {
            appRatingDialog.mNeverRateButtonText = neverRateButtonText;
            appRatingDialog.onRateNeverDialogListener = onNeverRateClickListener;
            return this;
        }

        // ...
    }
}

EDIT 3:

To open Play Store and notify a listener simultaneously:

public class AppRatingDialog extends AppCompatDialog implements View.OnClickListener {
    //...
    private OnRatingDialogListener onRatingDialogListener;
    private OnRatingDialogListener onRateLaterDialogListener;
    private OnRatingDialogListener onRateNeverDialogListener;

    //...

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.dialog_rating_button_never_rate) {
            savedNeverShow();
            dismiss();

            if (onRateNeverDialogListener != null) {
                onRateNeverDialogListener.onClick();
            }
        } else if (v.getId() == R.id.dialog_rating_button_rate_later) {
            dismiss();
            incrementLaunchCount(true);

            if (onRateLaterDialogListener != null) {
                onRateLaterDialogListener.onClick();
            }
        } else if (v.getId() == R.id.dialog_rating_button_rate) {
            savedNeverShow();
            dismiss();

            openPlayStore();

            if (onRatingDialogListener != null) {
                onRatingDialogListener.onClick();
            }   
        }
    }

    // ...
}

Please follow these steps: In the library module look for class called AppRatingDialog.java add YOURACTIVITY.goToPickerActivity(**boolean**); to each view of onClick (at line 171) method like this one:

@Override
public void onClick(View v) {
    if (v.getId() == R.id.dialog_rating_button_never_rate) {
        YOURACTIVITY.goToPickerActivity(false); 
        savedNeverShow();
        dismiss();

        if (onRatingDialogListener != null) {
            onRatingDialogListener.onClick();
        }
    } else if (v.getId() == R.id.dialog_rating_button_rate_later) {
        YOURACTIVITY.goToPickerActivity(false);
        dismiss();
        incrementLaunchCount(true);

        if (onRatingDialogListener != null) {
            onRatingDialogListener.onClick();
        }
    } else if (v.getId() == R.id.dialog_rating_button_rate) {
        YOURACTIVITY.goToPickerActivity(true);
        savedNeverShow();
        dismiss();

        if (onRatingDialogListener != null) {
            onRatingDialogListener.onClick();
        }
    }
}

In your activity modify your method goToPickerActivity() to be something like this:

private static void goToPickerActivity(boolean isRate) {
    Intent intent = new com.sucho.placepicker.PlacePicker.IntentBuilder()
        .onlyCoordinates(true) //Get only Coordinates from Place Picker
        .showLatLong(true)
        .setLatLong(40.71274, -74.005974)  // Initial Latitude and Longitude the Map will load into (NYC coordinates)
        .setMapZoom(2.5f)  // Map Zoom Level. Default: 14.0
        .build(this);
    if (isRate){
        startActivityForResult(intent, Constants.PLACE_PICKER_REQUEST_RATE);
    } else {
        startActivityForResult(intent, Constants.PLACE_PICKER_REQUEST);
    }

in method onActivityResult check the request code if it's equal to Constants.PLACE_PICKER_REQUEST_RATE then call method openStore():

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == Constants.PLACE_PICKER_REQUEST_RATE) {
        //other code ...
        openStore();
        }
    } 
}

private void openStore() {
    final Uri storeUri = Uri.parse(mStoreLink); // your app store listing link
    try {
        startActivity(new Intent(Intent.ACTION_VIEW, storeUri));
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
}

You can add handler to openStore() if you want to wait for certain time before go to store page:

Handler mHandler = new Handler();
mHandler.postDelayed(() -> {
    openStore();
}, 1000); // wait for 1 second

As you are able to edit the library code, to match up your requirement, first of all, change appratingdialog library code. Go to AppRatingDialog.java and inside onClick(View v) modify the rating button condition like below:

if (v.getId() == R.id.dialog_rating_button_rate) {
    savedNeverShow();
    dismiss();

    if (onRatingDialogListener != null) {
        onRatingDialogListener.onClick();
        openPlayStore();    // Just added this extra line here, others are kept in place
    } else {
        openPlayStore();
    }
}

Now, inside your app activity, create custom click listener for rating button using setRateButtonText like below:

AppRatingDialog appRatingDialog = new AppRatingDialog.Builder(MainActivity.this)
                .setTriggerCount(3)
                .setRepeatCount(4)
                .setRateButtonText("Rate Now", clickListener)
                .build();
appRatingDialog.show();

Finally, implement the click listener inside your app activity having the PlacePicker intent inside onClick like below:

OnRatingDialogListener clickListener = new OnRatingDialogListener() {
    @Override
    public void onClick() {
        goToPickerActivity();
    }
};

You can simply write a class and extend from AppRatingDialog. then by overriding onClick method you can do what you want. then by calling super.onClick you'll run the library onClick method.

@Override
    public void onClick(View v) {
        if (v.getId() == R.id.dialog_rating_button_rate) {
            goToPickerActivity();
        }
        super.onClick(v);
    }

You actually seem to have this already answered in your original post.

startActivityForResult(intent, Constants.PLACE_PICKER_REQUEST);

public void goToPickerActivity() {
    Intent intent = new com.sucho.placepicker.PlacePicker.IntentBuilder()
            .onlyCoordinates(true) //Get only Coordinates from Place Picker
            .showLatLong(true)
            .setLatLong(40.71274, -74.005974)  // Initial Latitude and Longitude the Map will load into (NYC coordinates)
            .setMapZoom(2.5f)  // Map Zoom Level. Default: 14.0
            .build(this);

    startActivityForResult(intent, Constants.PLACE_PICKER_REQUEST);
  }

So override onActivityResult() from within there you can determine if the activity was successful and then do what you need like below

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (!(requestCode == 0 && resultCode == RESULT_OK)){
        onBackPressed();
    }else{
        goToPickerActivity()
    }
}

Try to update these following dependencies in your build.gradle project level:

classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:$the_latest_version'
Related