Adding data bundle to Intent : Intent « Core Class « Android






Adding data bundle to Intent

    


package app.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 class Activity1 extends Activity {
  OnClickListener listener1 = null;
  Button button1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.row);
        listener1 = new OnClickListener() {
      public void onClick(View v) {
        Bundle bundle = new Bundle();
        bundle.putString("store", "Activity1");
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        finish();
      }
    };
    button1 = (Button) findViewById(R.id.button3);
    button1.setOnClickListener(listener1);
    String data=null;
     Bundle extras = getIntent().getExtras();
          if (extras != null) {
               data = extras.getString("activityMain");
          }
    setTitle("Activity1:"+data);
    }
}

 class Activity2 extends Activity {
  OnClickListener listener = null;
  Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_edit);
        listener = new OnClickListener() {
      public void onClick(View v) {
        finish();
      }
    };
    button = (Button) findViewById(R.id.button4);
    button.setOnClickListener(listener);
    setTitle("Activity2");
    }
}
public class Test extends Activity {
  OnClickListener listener1 = null;
  OnClickListener listener2 = null;
  Button button1;
  Button button2;
  static final int REQUEST_CODE = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listener1 = new OnClickListener() {
      public void onClick(View v) {
        Intent intent1 = new Intent(Test.this, Activity1.class);
        intent1.putExtra("activityMain", "activityMain");
        startActivityForResult(intent1, REQUEST_CODE);
      }
    };
    listener2 = new OnClickListener() {
      public void onClick(View v) {
        setTitle("ActivityMain");
        Intent intent2 = new Intent(Test.this, Activity2.class);
        startActivity(intent2);

      }
    };
    setContentView(R.layout.main);
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(listener1);
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(listener2);
    setTitle("ActivityMain");
  }
    
    @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
      if (resultCode == RESULT_CANCELED)
        setTitle("OK");
      else if (resultCode == RESULT_OK) {
        String temp=null;
         Bundle extras = data.getExtras();
              if (extras != null) {
                temp = extras.getString("store");
              }
        setTitle(temp);
      }
    }
  }
}

//  layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Button android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="??button1" />
  <Button android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="??button2" />
</LinearLayout>


//  layout/row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
  <Button android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="??button3" />

</LinearLayout>



//  layout/add_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
  <Button android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="??button4" />
</LinearLayout>

   
    
    
    
  








Related examples in the same category

1.Using Intent to open other Activity
2.Create Intent to open a Uri
3.extends IntentService
4.Phone Intent
5.Map Intent
6.Market Intent
7.Load Activity with Intent
8.Using Intent to make another phone call
9.extends IntentService to create your own Intent
10.Using Intent to show other Activities
11.Start Intent with Utility class
12.Load library Activity with Intent
13.Capture Image with Intent
14.Intent.ACTION_MEDIA_MOUNTED
15.Using Intent to record audio
16.Video Player Intent
17.Video Capture Intent
18.Implementing an application service that will run in response to an alarm, allowing us to move long duration work out of an intent receiver.
19.Example of various Intent flags to modify the activity stack.
20.Sample code that invokes the speech recognition intent API.
21.extends IntentService to upload a file
22.factory class for generating various intents
23.Open Web Page Intent
24.Is Intent Available
25.start MMS Intent
26.Access the Internet
27.Pdf viewer
28.Rotation One Demo
29.Media activity