Goran_1992 Goran_1992 - 1 year ago 128
Android Question

Why my Bundle extras does not exist?

I am building an app and I am using FCM notifications. When user clicks on notification I want to send him to the Fragment1(that is the name of the fragment) on Main Activity. I do that with intent.putExtra but when in Main Activity I log Bundle extras=getIntent().getExtras() I have null pointer exception. Why I don't have anything in putExtra?

Here is myFireBaseMessagingService

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("OnMessage", "Received");
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From " + remoteMessage.getFrom());
Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage);

}




private void sendNotification(RemoteMessage remoteMessage) {
Intent intent=new Intent(this, MainActivity.class);
intent.putExtra("action", "goToFragment1");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentText(remoteMessage.getNotification().getBody())
.setContentTitle("Asp")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());


}


And here is MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.dugme1);
btn2=(Button)findViewById(R.id.dugme2);
btn3=(Button)findViewById(R.id.dugme3);
dugme=(Button)findViewById(R.id.subscribe);
dugme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseMessaging.getInstance().subscribeToTopic("developeri");
Log.d("Bravo", "Subscribed to developeri!");
}
});


FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
StartFragment startFragment=new StartFragment();
ft.add(R.id.myFragment, startFragment);
ft.commit();
btn1.setOnClickListener(btnOnClickListener);
btn2.setOnClickListener(btnOnClickListener);
btn3.setOnClickListener(btnOnClickListener);
Intent intent=getIntent();
Bundle extras=getIntent().getExtras();
Log.d("Bundle", "Bundle is here");
Log.d("Extras", extras.getString("action"));
if (extras!=null && extras.containsKey("action") && extras.getString("action").contains("goToFragment1")){
getSupportFragmentManager().beginTransaction().replace(R.id.myFragment, new Fragment1()).commit();
Log.d("MainActivity", "IF IS HERE");

}

}
Button.OnClickListener btnOnClickListener= new Button.OnClickListener() {
@Override
public void onClick(View view) {
Fragment newFragment;
if (view==btn1){
newFragment =new Fragment1();

}else if(view==btn2){
newFragment =new Fragment2();
}else if (view==btn3){
newFragment =new Fragment3();
}else{
newFragment=new StartFragment();
}
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.myFragment, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();


}
};


}

My logcat


06-07 09:22:49.598 23913-23913/com.example.dev3.fragmenti
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.dev3.fragmenti/com.example.dev3.fragmenti.MainActivity}:
java.lang.NullPointerException
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$700(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at
com.example.dev3.fragmenti.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java) 
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java) 
at android.app.ActivityThread.access$700(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java) 
at android.os.Handler.dispatchMessage(Handler.java) 
at android.os.Looper.loop(Looper.java) 
at android.app.ActivityThread.main(ActivityThread.java) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
at dalvik.system.NativeStart.main(Native Method)

Answer Source

On create is only called when activity is launched. It main remain "alive" while receiving your notification. You should therefore have your code handling the extra in:

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

        if (intent.hasExtra("action")) {
            String action =  intent.getExtras().getString("action");
            // Do what ever is needed
            if (action.equalsIgnoreCase("gotToFragment1")) { 
                getSupportFragmentManager().beginTransaction().replace(R.id.myFragment, new Fragment1()).commit();
            }
        }
    }

Add the onNewIntent() method to your MainActivity (the activity receiving the new Intent). This will be called if your activity is already started.