I'll admit it. I don't get the implementation of Bundles and Intents on Android.
Even after reading for so long, its confusing me a lot.
I understand on a basic level that :
Intents are like phonecalls to another person in a same block.
Bundles are temporary storage packages that get deleted when the application is exited.
What I don't get, is how to use them. I see so many examples explaining how to instantly put them into your activity, but no further explanation at all.
Something like this question http://stackoverflow.com/questions/768969/passing-a-bundle-on-startactivity/819427#comment17818750_819427
which provides the code and a very basic explanation, but doesn't really elaborate on how to use it.
I'm using what the solution in what that question recommends, but my application always crashes because I probably don't even understand what i'm actually doing.
Is there a detailed explanation of Android Bundles / Intents that someone can provide or link me to?
I've seen examples of Android Bundles and Intents and the block of code that they always suggest.
What I don't understand, is how to use it. Where do you create the 'Create a new Bundle'?
'Example.class' in Intent mIntent = new Intent(this, ActivityA.class);
, where does it direct to?
EDIT : CapDroid asked me to provide codes, so here it is -
Let's say I have two classes right now, ActivityA
and ActivityB
.
public class ActivityA extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notify_main);
// Do I create the bundle here? [APP CRASHES]
// Intent mIntent = new Intent(this, ActivityA.class);
// Bundle mBundle = new Bundle();
// mBundle.extras.putString(key, value);
// mIntent.putExtras(mBundle);
}
// onClick Execute Method (TextView acting as a Spinner)
public void DatePicker(View v) {
// Create a new DialogFragment (DateFragment)
DialogFragment TimeFragment = new ActivityB();
DateFragment.show(getSupportFragmentManager(), "DatePicker");
// Opens up ActivityB, user selects date
// ActivityB displays the selected date back in ActivityA
}
// onClick Execute Method (Button)
public void Save(View v) {
// Method to save all the data (ActivityB result, in future ActivityC, ActivityD, etc) in a database
// String dayValue = getIntent().getExtras().getString(APP_KEY_DAY)
// String mthValue = getIntent().getExtras().getString(APP_KEY_MTH)
// String yearValue = getIntent().getExtras().getString(APP_KEY_YEAR)
}
}
The application consists of a TextView (which is acting as a Spinner for Date Input).
Right now I want to save the Date that the user selects, either in ActivityB (Raw Input Data) or ActivityA (get the information from TextView). Most preferably in ActivityB though, raw data is easier to handle than a string (TextView).
This is ActivityB :
public class ActivityB extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Saves year month day into a Bundle, send it back to ActivityA [APP CRASHES]
// This is what i did :
// Intent mIntent = new Intent(this, ActivityA.class);
// Bundle extras = mIntent.getExtras();
// extras.putString(APP_KEY_DAY, day);
// extras.putString(APP_KEY_MTH, month);
// extras.putString(APP_KEY_YEAR, year);
//Changes the values on TextView.
((TextView)this.getActivity().findViewById(R.id.ActivitySpinner)).setText(day + " " + month + " " + year);
return;
}
I'm thinking that the user is able to save the selected Date in ActivityB in a Bundle, then transfer the Bundle back into ActivityA. This is because I am going to have multiple values, not just one TextView.
I hope this explanation helps.