Intent




An Intent is a class that holds information about how to run another Activity, ...

Example:

public void myButtonClick(View v)
{
    Intent intent = new Intent();
    intent.setClassName("com.mypackagename", "com.mypackagename.NameOfClass");
    intent.putExtra("akey", "the data");
    startActivity(intent);
}



In the new Activity, in the OnCreate method do the following:

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String newText = bundle.getString("akey");




add

<activity android:name=".xxxxxx" />

to the manifest

where xxxxxx is replaced with the name of the class.



This goes back to the caller Activity without calling the OnCreate method.

Intent newIntent=new Intent("*android*.intent.action.OriginalClass");

newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(newIntent);




//so this are two calls with a different request code

startActivityForResult(intent, CREATE_REQUEST_CODE);

startActivityForResult(intent, EDIT_REQUEST_CODE);


// write this in the same class where you started the intent
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CREATE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//ACT
}
}
}


// do this in the class that you called
setResult(RESULT_CANCELED, null);
finish();

//or

setResult(RESULT_OK, null);
finish();

// use these constants
RESULT_CANCELED : Standard activity result : operation canceled.
RESULT_FIRST_USER : Start of user-defined activity results.
RESULT_OK : Standard activity result : operation succeeded.


Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.de"));


// To call an Activity and get back a result:
public void onClick(View view) {
       Intent i = new Intent(this, ActivityTwo.class);
       i.putExtra("sendkey1", "some data");
       i.putExtra("sendkey2", "some data");
       // Set the request code to any code you like, you can identify the
       // callback via this code
       startActivityForResult(i, REQUEST_CODE);
}


In the called Activity, in the OnCreate method do the following:

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String newText = bundle.getString("sendkey1");



// Put this in the called Activity:
@Override
public void finish() {
       // Prepare data intent
       Intent data = new Intent();
       data.putExtra("key1", "some data");
       data.putExtra("key2", "some data");
       // Activity finished ok, return the data
       setResult(RESULT_OK, data);
       super.finish();
}


// Put this in the Original class that does the calling:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
         if (data.hasExtra("key1")) {
          String theData = data.getExtras().getString("key1");
          // do whatever with the data
         }
       }
}