4

Always getting bundle null....

This is my code;

  edt=(EditText)findViewById(R.id.edt);

    String msg=edt.getText().toString();

    //create Intent
     Intent myIntent = new Intent(this, ScondActivity.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    //Here I pass Data;
    myIntent.putExtra("msg",msg);

    //Initialize PendingIntent
    pendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //Initialize NotificationManager using Context.NOTIFICATION_SERVICE
    notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    //Create listener to listen button click
    OnClickListener listener = new OnClickListener() {
        public void onClick(View view) {
            //Prepare Notification Builder
            notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
                    .setContentTitle("Notification Demo").setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pendingIntent)
                    .setContentText(edt.getText().toString());
            //add sound
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(uri);
            //vibrate
            long[] v = {500,1000};
            notificationBuilder.setVibrate(v);
            notificationBuilder .setContentIntent(pendingIntent);
            notificationManager.notify(1, notificationBuilder.build());
        }
    };
    Button btn = (Button)findViewById(R.id.button);
    btn.setOnClickListener(listener);

ScondActivity.java

public class ScondActivity extends Activity {
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.msglayout);

}

@Override
public void onNewIntent(Intent intent){

    Bundle extras = getIntent().getExtras();
    textView = (TextView)findViewById(R.id.msg);

    if(extras != null) {
        String tabNumber = extras.getString("msg");
        textView.setText(tabNumber);
        Log.d("TEMP", "Tab Number: " + tabNumber);

    } else {
        Log.d("TEMP", "Extras are NULL");
    }
}
 }
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57

2 Answers2

1

Get your data in onNewIntent(Intent intent) method.

@Override
protected void onNewIntent(Intent intent) 
 {
   super.onNewIntent(intent);
   //code
 }
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • What do you mean by not calling ? How do you know that it's not calling ? – Piyush Aug 11 '17 at 06:31
  • cozzz I define TextView in OnCreate() and I got nothing in TextView.So we can expect this function is not calling – Vishal Vaishnav Aug 11 '17 at 06:32
  • This is override method and you need to implement in your activity. Also retrieve data through `String tabNumber = intent.getStringExtra("msg"); textView.setText(tabNumber);` in `onNewIntent` method. – Piyush Aug 11 '17 at 06:34
  • still I always get Bundle null...see my updated code – Vishal Vaishnav Aug 11 '17 at 06:37
  • Change this `Bundle extras = intent.getExtras();` And you'r passing data in `NotificationActivity` and retrieving in `ScondActivity` ? – Piyush Aug 11 '17 at 06:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151679/discussion-between-vishal-vaishnav-and-piyush). – Vishal Vaishnav Aug 11 '17 at 06:44
  • @Piyush hello sir could you please check [my question](https://stackoverflow.com/q/49891050/8027365) I'm stuck in this from last 1 day – Srinivas Nahak Apr 18 '18 at 13:04
  • Did you call `onNewIntent(Intent intent)` method in your class ?? – Piyush Apr 27 '18 at 07:46
0

I just solve my problem.....I defined Intent for SecondActivity out side the button click event .So, I got bundle blank.My code is look like this....

 edt=(EditText)findViewById(R.id.edt);

    //create Intent

    //Create listener to listen button click
    OnClickListener listener = new OnClickListener() {
        public void onClick(View view) {
            //Prepare Notification Builder

            Intent myIntent = new Intent(MainActivity.this, NotificationActivity.class);
            myIntent.putExtra("msg",edt.getText().toString());
            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );

            //Initialize PendingIntent
            pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            //Initialize NotificationManager using Context.NOTIFICATION_SERVICE
            notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);


            notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
                    .setContentTitle("Notification Demo").
                            setSmallIcon(R.mipmap.ic_launcher).
                            setContentIntent(pendingIntent)
                    .setContentText(edt.getText().toString());
            //add sound
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(uri);
            notificationBuilder .setWhen(System.currentTimeMillis());
            //vibrate
            long[] v = {500,1000};
            notificationBuilder.setVibrate(v);
            notificationManager.notify(1, notificationBuilder.build());
        }
    };
    Button btn = (Button)findViewById(R.id.button);
    btn.setOnClickListener(listener);
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57