0

I have 2 activities in my application, Activity1 and Activity2.When the app launches Acitivity1 is the the one that is called.Clicking on a button in Activity1 should take you to Activity2.

In Acivity2,some data processing is done then i send back data to Activity1 using an intent like this:

Intent in=new Intent(getApplicationContext(), Activity1.class);
  in.putExtra("data", data);
    startActivity(in);

Then getting back to Activity1 i obtain the intent data:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);

    String data =(getIntent().getExtras().getString("data"));

The problem here is that the first time the app launches it checks for the intent data and it does not exist so i get the nullpointerexception error.how can i make sure it checks for the intent data when Activity2 is the previous class?

r_allela
  • 792
  • 11
  • 23

5 Answers5

2

You can check if the intent is existing using:

getIntent().hasExtra("data");

This will return you a boolean.

Also if oyu want to return some datas to the first activity, your should start the second one with startActivityForResult

Damien R.
  • 3,383
  • 1
  • 21
  • 32
0
if (extras != null) {
    if (extras.containsKey("data")) {
        boolean hasData = extras.getBoolean("data", false);

        // TODO: Do something with the value of "data".
    }
}
bhaskarc
  • 9,269
  • 10
  • 65
  • 86
0

put a check over it

if(getIntent().getExtras()!=null){

    String data =(getIntent().getExtras().getString("data"));

}
Manishika
  • 5,478
  • 2
  • 22
  • 28
  • what if you are passing data from a third activity to MainActivity with a different key then `getIntent().getExtras()` will it be null? – Raghunandan Jan 20 '14 at 14:20
0

Follow the following steps

1: from your activity1's button click call as follow

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

2: In your Activity2 set the data which you want to return back to Activity1 as follow and if you don't want to return back don't set anything.

Intent returnIntent = new Intent();
 returnIntent.putExtra("result",result);
 setResult(RESULT_OK,returnIntent);     
 finish();

if you don't want to return data:

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

3: now again in your Activity1 handle the return data as follow

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1) {

     if(resultCode == RESULT_OK){      
         String result=data.getStringExtra("result");          
     }
     if (resultCode == RESULT_CANCELED) {    
         //Write your code if there's no result
     }
  }
}

EDIT:

and if you strictly want to use your own method as in your question try this:

in your Activity1 you obtain the intent data as: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity1);

String data ="default value";
try{
   data =(getIntent().getExtras().getString("data"));
}catch(Exception e){
}

}

dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
0

use ActvityForResult. replace these codes.

in ACTVITY1:

put this after onCreate:

@Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);

String name =data.getStringExtra("data");
if(resultCode == RESULT_OK){
    switch(requestCode){
    case 2:
        if(resultCode == RESULT_OK){
            Toast.makeText(this, name, Toast.LENGTH_LONG).show();
        }
    }
}

in ACTIVITY2:

Intent intent=new Intent();  
intent.putExtra("data",data);  
setResult(2,intent);  
finish();
Amir
  • 391
  • 3
  • 8