Jibin Joseph Jibin Joseph - 5 months ago 36
Android Question

Method onNewIntent not executing after destroying Activity?

I have developed an app to upload images to a server(like facebook Image Upload),I have used a Background Service to upload Image,when upload starts ,a notification progress will be displayed,When user cliks on the notification,I want to show a popup window whether to Cancel or continue upload,So I have written onNewIntent method in an activity,when user clicks on the notification ,that activity(Say activity ImageViewActivity) will be started and I want to show the popup.But the onNewIntent method is not executing instead the onCreate method of ImageViewActivity is executed.Why this happened??, the launch mode of that activity is SingleTop.Can anybody help me??

Notification and Pending Intent in MyService.java

private void startNotification() {


NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence notiText = "Your notification from the service";
long meow = System.currentTimeMillis();

Notification notification = new Notification(icon, notiText, meow);

Context context = getApplicationContext();
CharSequence contentTitle = "Your notification";
CharSequence contentText = "Some data has arrived!";

//PENDING INTENT
Intent notificationIntent = new Intent(this, ImageViewActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

int SERVER_DATA_RECEIVED = 1;
notificationManager.notify(SERVER_DATA_RECEIVED, notification);


}


onNewIntent Method in ImageViewActivity.java

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


Here I am calling initiatePopupWindow() Method.

public void initiatePopupWindow() {
try {

LayoutInflater inflater;
inflater = (LayoutInflater) ImageViewActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupmenu, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

// pwindo.setFocusable(true);
pwindo.setOutsideTouchable(true);
// pwindo.showAsDropDown();
Button btnStopUpload = (Button) layout.findViewById(R.id.btn_upload);
btnStopUpload.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// mNotifyManager.cancel(MY_NOTIFICATION_ID);
// mTask.cancel(true);
Log.e(TAG, "Notification Cancelled ");
// mTask.cancel(true);
/* Toast.makeText(getApplicationContext(),
"Upload Cancelled", Toast.LENGTH_SHORT).show();*/
Intent i=new Intent(ImageViewActivity.this,MyService.class);
//stopService(i);
ImageViewActivity.this.stopService(i);
ImageViewActivity.this.finish();
}
});


Button btnCancelPopup = (Button) layout.findViewById(R.id.btn_cancel);

btnCancelPopup.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
pwindo.dismiss();

}
});
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"POPUP Rejected", Toast.LENGTH_SHORT).show();
Log.e("COOKBOOK", "I got an error", e);
e.printStackTrace();
}
}


Logcat

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference
at project1.me.com.cookbookintent.ImageViewActivity.initiatePopupWindow(ImageViewActivity.java:266)
at project1.me.com.cookbookintent.ImageViewActivity.onCreate(ImageViewActivity.java:90)
at android.app.Activity.performCreate(Activity.java:5953)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)


OnCreate ()

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

Log.i(TAG, "Activity created");

try{
Intent intent = getIntent();

if (intent.getBooleanExtra("FROM_NOTIFICATION",false)) {
Toast.makeText(getApplicationContext(),
"Reject", Toast.LENGTH_SHORT).show();
initiatePopupWindow();
}

}
// int value= intent.getExtras().getInt("FROM_NOTIFICATION");
// if(value==2){


catch (Exception e){

Log.i(TAG, "Rejected");
Toast.makeText(getApplicationContext(),
"Reject", Toast.LENGTH_SHORT).show();
}

mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());

//Image from sdcard
try {
Intent intent1 = getIntent();
picturePath = intent1.getExtras().getString("key");
state = intent1.getExtras().getInt("first_state");
imageView = (ImageView) findViewById(R.id.imgView);
decodeFile(picturePath);
Toast.makeText(getApplicationContext(),
"Gallery State" + picturePath, Toast.LENGTH_SHORT).show();

} catch (Exception e) {

Toast.makeText(getApplicationContext(),
"pic not found", Toast.LENGTH_SHORT).show();
}

///Camera Image
try {
if (picturePath == null) {
Intent camIntent = getIntent();
picturePath= camIntent.getExtras().getString("filePath");
state = camIntent.getExtras().getInt("first_state");
imageView = (ImageView) findViewById(R.id.imgView);
decodeFile(picturePath);
Toast.makeText(getApplicationContext(), "PATH" + picturePath, Toast.LENGTH_SHORT).show();
bitmap = ShrinkBitmap(picturePath, 300, 300);


}
} catch (Exception e) {

Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
}
uploadPicture();

}

Answer

This is exactly what is expected: if the Activity has not been destroyed, you get the Intent in your onNewIntent, otherwise a new Activity will be created and you have to handle the Intent inside the onCreate. A simple snippet of code to create a PopupWindow:

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

    View root = findViewById(R.id.parentView);
    root.post(new Runnable() {
        @Override
        public void run() {
            LayoutInflater inflater = (LayoutInflater)
                    MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.popup_view, null, false);
            PopupWindow popupWindow = new PopupWindow(view, 300, 300, true);
            popupWindow.showAtLocation(findViewById(R.id.test), Gravity.CENTER, 0, 0);
        }
    });
}

where R.id.parentView is one of the View of your Activity and R.layout.popup_view is your PopupWindow layout.

Comments