3

When app is closed and a notification comes, user clicks it and a specific user's Profile opens up. It has a parent activity (UsersActivity) but in this case when back button in the toolbar is clicked, the app is closed.

Back button is working as expected when user goes to profile activity from UsersActivity.

I want it to go parent activity even app starts with a notification. How can I do that?

here is my manifest:

<activity
    android:name=".UsersActivity"
    android:parentActivityName=".MainActivity"
    >

</activity>
<activity
    android:name=".ProfileActivity"
    android:parentActivityName=".UsersActivity"
    >

This is ProfileActivity:

public class ProfileActivity extends AppCompatActivity {

    private Toolbar mToolbar;


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


        mToolbar = findViewById(R.id.profile_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_gold_back);
        getSupportActionBar().setLogo(R.drawable.ic_logo_v);
        getSupportActionBar().setTitle(R.string.app_name); 
        getSupportActionBar().setSubtitle(R.string.profile_toolbar_title);

Notification's class:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String notification_title = remoteMessage.getData().get("title");
        String notification_message = remoteMessage.getData().get("body");

        String click_action = remoteMessage.getData().get("click_action");

        String from_user_id = remoteMessage.getData().get("from_user_id");



        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(notification_title)
                        .setContentText(notification_message);


        Intent resultIntent = new Intent(click_action);
        resultIntent.putExtra("user_id", from_user_id);


        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);




        int mNotificationId = (int) System.currentTimeMillis();

        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (mNotifyMgr != null) {
            mNotifyMgr.notify(mNotificationId, mBuilder.build());
        }


    }
}
hullabaloon
  • 654
  • 2
  • 7
  • 17
  • Possible duplicate of [Back to main activity from notification-created activity](https://stackoverflow.com/questions/13800680/back-to-main-activity-from-notification-created-activity) – ADM Mar 10 '18 at 02:56

2 Answers2

0

maybe your onBackPressed() method in your Activity is doing a finish(); of the current Activity and there is no other Activities in the background, so it will close the app.

To solve this you can always return to your MainActivity with an intent from your UserActivity

if the user opens the notifications and goes to their profiles you can implement a method in your ProfileActivity.class with this

@Override
public void onBackPressed() {

startActivity(new Intent(this,MainActivity.class));

}

Also in your code, make sure you are not doing a finish(); in your activity when you are closing it, because it will disapear from the current stack and then if you enter again to your app, like i said, you will be closing the app since there is no Activities in the backgroud.

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if(keyCode!=KeyEvent.KEYCODE_BACK)return false;

    //enter the required code here to go to the necessary activity
    return true;
}
SadeepDarshana
  • 1,057
  • 18
  • 34