1

I am working on notification and I have a problem. I have an activity already open when I click on notification I don't want to open it again just update the current activity.

if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
 Intent pushNotification = null;
                if (NotificationType != 0 && NotificationType != 2 && NotificationType != 5 && NotificationType != 26) {
                    pushNotification = new Intent(getApplication(), SplashScreen.class);
                    pushNotification.putExtra("NotificationType", NotificationType);
                    pushNotification.putExtra("ReferenceID", ReferenceID);
                    pushNotification.putExtra("NotificationID", ReferenceID);
                    pushNotification.putExtra("isread", ReferenceID);
                    showNotificationMessage(getApplicationContext(), title, message, time, pushNotification);
                } else if (NotificationType == 0 || NotificationType == 2 || NotificationType == 5 || NotificationType == 26) {
                    showNotificationMessageWithNoAction(getApplicationContext(), title, message, title, null);
                }
}

can Anyone tell me how I update the activity when I click on notification?

Egon Allison
  • 1,329
  • 1
  • 13
  • 22
  • Have you checked this one https://stackoverflow.com/questions/6547969/android-refresh-current-activity/#6547998 – Md. Sabbir Ahmed Mar 27 '19 at 13:31
  • @SabbirAhmed i don't but i don't want to finish and restart it to again... i am looking for interaction or something like that which can be update my activity without restart. thank's – Muhammad Imran Mar 27 '19 at 13:34

3 Answers3

1


You just need to declare the launchMode to the singleTask to make ensure that multiple same screens not will open.

There are four launch modes for activity. They are:
1. Standard
2. SingleTop
3. SingleTask
4. SingleInstance

Please refer this link Click here

<activity android:name="YOUR_SPLASH_ACTIVITY"
            android:launchMode="singleTask"

            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

And in the Java code , you just override the onNewIntent method , to refresh activity,

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    /**
     * HERE YOU JUST REFRESH , YOUR ACTIVITY 
     */

}
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
  • ohh thanks its work after adding this code android:launchMode="singleTask" & @Override protected void onNewIntent(Intent intent) { if (intent != null) { RefrenceId = intent.getIntExtra("ReferenceID", 0); } – Muhammad Imran Mar 27 '19 at 13:52
  • 1
    You can also use the `intent. hasExtra("ReferenceID);`, for it.... happy to help you @MuhammadImran .. Keep happy coding – Ravindra Kushwaha Mar 27 '19 at 13:53
0

I don't know if this is what you need but you can do this

  finish();
  startActivity(getIntent());

Let me know what you really need.

EDIT:

If you need to keep your activity state intact, you can do this

  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("MyString", "Welcome back to Android");
 }

This will save your activity state

And then you retrieve UI state like this

  @Override
  public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    String myString = savedInstanceState.getString("MyString");
  }
Amine
  • 2,241
  • 2
  • 19
  • 41
  • i don't want to finish and restart it to again because i have too much network requests on start of activity that's why... i am looking for interaction or something like that which can be update my activity without restart. thank's – Muhammad Imran Mar 27 '19 at 13:36
0

Best way is to make use of the onNewIntent(Intent) method of your Activity.

You can use the Intent parameter of this method to get your Intent Extras, because getIntent() will give the Intent that started the Activity in the first place.

public class MainActivity extends Activity {
    public void onCreate(Bundle SavedInstanceState) {
    //Initial loading
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if(intent.getStringExtra("methodName").equals("myMethod")) {
            //Update the existing screen
        }
    }
}
Housefly
  • 4,324
  • 11
  • 43
  • 70