Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle an intent which does not have data at first call of an activity?

I have two activities; Home is my first activity and Settings is my second activity.

The Settings activity is called from the menu of the Home activity, and returns data to home activity through intent.

But first time when I run the android application, the intent will be empty because the second activity is not yet called, so I get exception there (Java null pointer exception).

Can anybody help me out to handle this solution?

EDIT: Code

First Activity:

public class LoginActivity extends Activity {

    public static final int SETTINGS_ID = 1;
    Intent intn;

    EditText edt1;
    EditText edt2;
    String user, pass;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        intn= new Intent(this,SettingsActivity.class);
    }

    private class HttpConnectionRequest extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void...params) {
            // TODO Auto-generated method stub
            //Code not included
        }
        return null;
    }
}

public void login(View V){

    edt1 = (EditText) this.findViewById(R.id.editText1);
    edt2 = (EditText) this.findViewById(R.id.editText2);
    user = edt1.getText().toString();
    pass = edt2.getText().toString();
    System.out.println("Username-->" + user);
    System.out.println("Password-->" + pass);
    String url="";
    url = getIntent().getExtras().getString("serverurl");
    System.out.println("url----->"+url);

    if(((null==user) || ("".equals(user))) && ((null==pass) || ("".equals(pass)) )){
        Toast.makeText(getApplicationContext(), "Please provide username and password", Toast.LENGTH_SHORT).show();  
    }
    else if((null==user) || ("".equals(user))){
        Toast.makeText(getApplicationContext(), "Please provide username", Toast.LENGTH_SHORT).show();  
    }
    else if ((null==pass) || ("".equals(pass))){
        Toast.makeText(getApplicationContext(), "Please provide password", Toast.LENGTH_SHORT).show();  
    }

    if ((null==url) || ("".equals(url))) {
        Toast.makeText(getApplicationContext(), "Please provide the Server url Settings->Server URL->Save", Toast.LENGTH_SHORT).show();  
    }

    HttpConnectionRequest conn= new HttpConnectionRequest();
    conn.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, SETTINGS_ID, 0, "Settings");
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.layout.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case SETTINGS_ID:
            //this.finish();
            this.startActivity(intn);
            System.out.println("This Invoked . . .");
            break;
        }
        return false;
    }
}

Second activity

public class SettingsActivity extends Activity {

    EditText edt1;
    Intent intn;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        intn = new Intent (this,LoginActivity.class);
    }

    public void save(View v){
        edt1= (EditText) this.findViewById(R.id.serverurl);
        String url = edt1.getText().toString();
        intn.putExtra("serverurl",url);
        startActivity(intn);        
    }
}
like image 788
DroidGirl Avatar asked Feb 07 '12 14:02

DroidGirl


People also ask

How could you pass data between activities without Intent?

This example demonstrate about How to send data from one activity to another in Android without intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you check which Intent started the activity?

Use startActivityForResult in your calling activity to start the activity: ActivityCompat. startActivityForResult(this, new Intent(this, MyActivity. class), 0, null);


2 Answers

You can do a check for the resultCode as below.

http://developer.android.com/guide/topics/fundamentals/activities.html#StartingAnActivityForResult

Also, can you identify where exactly you get the exception? (post some code maybe)

EDIT:

Ok, I still donT know where you are calling your login() method but you can do this check to avoid null exceptions in the line you mentioned:

if( getIntent().getExtras() != null)
{
  //do here
}
like image 82
Orkun Ozen Avatar answered Nov 09 '22 22:11

Orkun Ozen


You can try:

if(intent.getExtras() == null) {
   //Do first time stuff here
} else {
  //Do stuff with intent data here
}

If you are passing back different intent values every time, you can check if your Bundle object (returned with intent.getExtras()) has certain fields set using this:

Bundle extras = intent.getExtras()
if(extras.containsKey("keytocheck")) {
  //Do stuff because extra has been added
}
like image 25
onit Avatar answered Nov 10 '22 00:11

onit