Android getIntent() returns the first intent

android intent
startactivityforresult
android activity lifecycle
intent flags
android startactivity
getserializableextra
action_media_scanner_scan_file
getintextra

I have developed an application to download a video file and store it in the SD card. In the process I also update the progress and status of the download as a status bar notification using the NotificationManager.

My class called the DownloadTask.java extends the AsyncTask. So here I update the progress using the onProgressUpdate() method where in I use the NotificationManager for the purpose. Everything works like a charm except, on completion of download I want to click the notification to open the specific video file. So this is what i have done:

mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

int icon = android.R.drawable.stat_sys_download_done;
long when = System.currentTimeMillis();

mNotification = new Notification(icon, "", when);
mContentTitle_complete = mContext.getString(R.string.download_complete);
notificationIntent = new Intent(mContext,OpenDownloadedVideo.class);
notificationIntent.putExtra("fileName", file);
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
mNotification.setLatestEventInfo(mContext, file, mContentTitle_complete, mContentIntent);
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, mNotification);

Note that the fileName and NOTIFICATION_ID are unique in my case.

The Activity OpenDownloadedVideo.java opens the file by:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {  
        fileName = getIntent().getExtras().getString("fileName");
        Intent i = new Intent(Intent.ACTION_VIEW);
        File videoFileToPlay = new File(Environment.getExternalStorageDirectory()+"/MyFolder"+"/"+fileName);
        i.setDataAndType(Uri.fromFile(videoFileToPlay), "video/*");
        startActivity(i);
        finish();
    } catch(Exception e) {
        //
    }
}

So when I download a video for the first time and click on the notification the appropriate video file will be opened. However next time when I download another video, and click on the notification the first file which was downloaded will be opened again.

This is because getIntent inside OpenDownloadedVideo returns the first Intent created and not the latest. How can I correct this?

Also, please note that the problem scenario exists when I download more than one video, e.g. if I download five different video files and there are five notifications in the status bar. The same file will be opened each time a notification is clicked.

actually you just need create PendingIntent with PendingIntent.FLAG_UPDATE_CURRENT ,like this:

mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Activity, onCreate(), Called when the activity is first created. This is where you Change the intent returned by getIntent() . Theme theme, int resid, boolean first). This is the scenario: The method getIntent() returns the FIRST intent than launch activity. So, when the activity is CLOSED (terminated) and the user clicks on the notification, it will run a new instance of the activity and getIntent() works as expected (Extras is not null).

/**
 * Override super.onNewIntent() so that calls to getIntent() will return the
 * latest intent that was used to start this Activity rather than the first
 * intent.
 */
@Override
public void onNewIntent(Intent intent){
    super.onNewIntent(intent); // Propagate.
    setIntent(intent); // Passing the new intent to setIntent() means this new intent will be the one returned whenever getIntent() is called.
}

Android Intents - Tutorial, package com.vogella.android.first; import android.app. skillLevel = getIntent(). here for brevity @Override public void finish() { Intent intent = new Intent(); skillLevel = skillLevel + 5; intent. Home » Android » Android getIntent().getExtras() returns null Android getIntent().getExtras() returns null Posted by: admin February 23, 2018 Leave a comment

From the Android Activity.onNewIntent() documentation

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

So when you get the new Intent, you need to explicitly set it as the activity intent.

Passing Data Using An Intent Object In Android Applications, In my previous article Return Data Using Intent Object in Android second activity, we first obtain the Intent object, using the getIntent() method. public Intent getIntent Retrieve the Intent as configured so far by the IntentBuilder. This Intent is suitable for use in a ShareActionProvider or chooser dialog. To create an intent that will launch the activity chooser so that the user may select a target for the share, see createChooserIntent().

@Alex and @Codinguser, thank you for your replies. Much appreciated. However I found a different answer that worked for me. When creating a PendingIntent for the Intent pass a unique value to it. In my case I was doing this:

mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); 

but now I'm using NOTIFICATION_ID since it was unique. I've changed the above call to:

mContentIntent = PendingIntent.getActivity(mContext, NOTIFICATION_ID,
                                           notificationIntent, 0);

That's all and it works.

I found some information on it in the question Mulitple Instances of Pending Intent

Using Intents to Create Flows, ActivityOne.java public void launchComposeView() { // first parameter is the context, onSubmit(View v) { // closes the activity and returns to first screen this. finish(); } void onCreate(Bundle savedInstanceState) { String username = getIntent(). Implicit intents specify the action which should be performed and optionally data which provides content for the action. If an implicit intent is sent to the Android system, it searches for all components which are registered for the specific action and the fitting data type.

What if you make your OpenDownloadedVideo activity SINGLE_TOP and override onNewIntent?

android.content.Intent.hasExtra java code examples, Best Java code snippets using android.content.Intent.hasExtra (Showing top 20 results out of new Intent(); Codota Icon hasExtra( EXTRA_FAB_ICON_RES_ID)) { return false; } final int color = intent. getString(" visibleLeakRefKey"); } else { Intent intent = getIntent(); if (intent. isEqualTo(23); Intent first = shadowActivity. OngoingStubbing intending (Matcher<Intent> matcher). Enables stubbing intent responses. This method is similar to Mockito.when and is particularly useful when the activity launching the intent expects data to be returned (and especially in the case when the destination activity is external).

android.content.Intent.getExtras java code examples, Best Java code snippets using android.content.Intent.getExtras (Showing top 20 results out of 6,498) getIntent(); assertThat(intentInFragment. How to return a result (startActivityForResult) from a TabHost Activity? getString("param_result" ); Log.d("FIRST", "result:"+result); } break; } } private void finishWithResult()� Uses of Intent in Android. There are three fundamental uses of intents: 1. To start an Activity. An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data along. 2. To start a Service

Every time getting null data in extra intent � Issue #205 � firebase , Android device: Nexus 5 or emulator Android OS version: Android N or @ Override public void onCreatet() { Bundle extras = getintent(). "android.intent.extra.genre" - The genre. "android.intent.extra.playlist" - The playlist. EXTRA_MEDIA_TITLE - The song name that the playlist is based on. QUERY (required) - A string that contains any combination of: the album, the artist, the genre, the playlist, or the title. This extra is always provided for backward compatibility: existing

Java Code Examples for android.app.Activity#getIntent, This page shows Java code examples of android.app. Activity#getIntent() getIntent(); if (intent == null) { return null; } AppLinkData appLinkData getStringExtra(BUNDLE_APPLINK_ARGS_KEY); // Try v2 app linking first AppLinkData�

Comments
  • also, to retrieve the new intent you have to override onNewIntent(), calling getIntent() will return the old one even if you use setIntent()
  • This is a great answer. I needed to basically use setIntent or call the super method for onNewIntent() to make sure getIntent returned the latest intent.
  • how do i do that ?? sorry Im quite new to this.
  • I don't know if it will help or not. In manifest editor you add SINGLE_TOP for your Activity launch mode. Then you override your Activity's onNewIntent() and put the same code in there as you have in onCreate
  • I have made the OpenDownloadedVideo activity SINGLE_TOP and inside the activity i have overriden the onNewIntent as well as you suggested.... But when the activity is launched or re-launched it doesn't seem to go inside the onNewIntent method.
  • @Bopanna Perhaps it's not relaunched but paused and then resumed?