Passing data or parameter to another Activity Android - using Bundle
This following step helps to share the content using bundle.
1. Create a two Activity [MainActivity & ChildActivity]
Activity : 1 - MainActivity
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Activity : 2 - ChildActivity
public class ChildActivity extends Activity {
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Put your code here
}
}
2. Define Your Shared collection
Bundle bundle = new Bundle();
bundle.putString("sample", "this is the test commands");
bundle.putString("sample1", "this is the test commands1");
3. Call the child Activity from mainactivity with the bundle
startActivity(new Intent(this, ChildActivity.class).putExtras(bundle));
4. Get the Value from MainActivity in ChildActivity
this.getIntent().getExtras().getString("sample")
5. Toast the Message
Toast.makeText(this, this.getIntent().getExtras().getString("sample"),
Toast.LENGTH_LONG).show();
Toast.makeText(this, this.getIntent().getExtras().getString("sample1"),
Toast.LENGTH_LONG).show();
Full Source :MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle bundle = new Bundle();
bundle.putString("sample", "this is the test commands");
bundle.putString("sample1", "this is the test commands1");
startActivity(new Intent(this, ChildActivity.class).putExtras(bundle));
}
}
-------ChildActivity.java----------
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
public class ChildActivity extends Activity {
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(this, this.getIntent().getExtras().getString("sample"),
Toast.LENGTH_LONG).show();
Toast.makeText(this, this.getIntent().getExtras().getString("sample1"),
Toast.LENGTH_LONG).show();
}
}
This following step helps to share the content using bundle.
1. Create a two Activity [MainActivity & ChildActivity]
Activity : 1 - MainActivity
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Activity : 2 - ChildActivity
public class ChildActivity extends Activity {
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Put your code here
}
}
2. Define Your Shared collection
Bundle bundle = new Bundle();
bundle.putString("sample", "this is the test commands");
bundle.putString("sample1", "this is the test commands1");
3. Call the child Activity from mainactivity with the bundle
startActivity(new Intent(this, ChildActivity.class).putExtras(bundle));
4. Get the Value from MainActivity in ChildActivity
this.getIntent().getExtras().getString("sample")
5. Toast the Message
Toast.makeText(this, this.getIntent().getExtras().getString("sample"),
Toast.LENGTH_LONG).show();
Toast.makeText(this, this.getIntent().getExtras().getString("sample1"),
Toast.LENGTH_LONG).show();
Full Source :MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle bundle = new Bundle();
bundle.putString("sample", "this is the test commands");
bundle.putString("sample1", "this is the test commands1");
startActivity(new Intent(this, ChildActivity.class).putExtras(bundle));
}
}
-------ChildActivity.java----------
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
public class ChildActivity extends Activity {
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(this, this.getIntent().getExtras().getString("sample"),
Toast.LENGTH_LONG).show();
Toast.makeText(this, this.getIntent().getExtras().getString("sample1"),
Toast.LENGTH_LONG).show();
}
}
No comments:
Post a Comment