2

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.

Kyle Yeo
  • 2,270
  • 5
  • 31
  • 53
  • http://stackoverflow.com/questions/768969/passing-a-bundle-on-startactivity/819427#comment17818750_819427%20which%20provides%20the%20code%20and%20a%20very%20basic%20explanation,%20but%20doesn%27t%20really%20elaborate%20on%20how%20to%20use%20it – Niranj Patel Nov 10 '12 at 04:52
  • I'm facing an issue where I don't understand how exactly the Intents and Bundles systems work. If you're talking about copy and pasting the code directly into my workspace, the application crashes. Most of the things I've tried still crash the application. I understand what you're saying here (by providing code), but would it help if I entered an entire file of functions and ask how? – Kyle Yeo Nov 10 '12 at 05:14
  • Intents tell the application what the next step is going to roughly do, such as open the next activity (like going from the homepage > display profile) – Kyle Yeo Nov 10 '12 at 05:32
  • This is one of my implementations, but this is where I copy and paste the entire 'create a bundle' portion into my workspace – Kyle Yeo Nov 10 '12 at 05:42
  • I'm trying to understand how it works, am i directing the Intent to a wrong activity, etc – Kyle Yeo Nov 10 '12 at 05:42
  • Please avoid long discussions in the comments. If you have any details to add, edit the question instead. – NullUserException Nov 10 '12 at 17:05

2 Answers2

0

Let me try to make this as simple as possible.

Intents: These are basically messages that gets passed between activities. Most common use case is to start a new activity.

For Instance: ActivityA needs to start ActivityB, it will call startActivity or better still call startActivityForResult.

In case you want to pass some data from ActivityA to ActivityB. You can use putExtra methods in the Intents to do that. This basically uses the bundles inside the intents.

One more important aspect is to how to return data from ActivityB to ActivityA. Here we use what we call as setResult in ActivityB before finishing the activity. The data is then accessible in onActivityResult

Check some examples here

PravinCG
  • 7,688
  • 3
  • 30
  • 55
  • Could you explain more? I know its going to take up a bit of your time but you'll be helping an Android Beginner a long way. – Kyle Yeo Nov 12 '12 at 01:56
  • @PravinCG why do you mention above "better still call startActivityForResult"? It seems more complicated to use all of the result and request codes whereas using putExtra() in a Bundle seems much more simple. Is there a big advantage to using startActivityForResult()? – AJW Apr 23 '19 at 02:14
0

Nowadays you would probably rather use Fragments and LocalBroadcastManager to pass information from an Activity to a Fragment and vice-versa.

AZOM
  • 265
  • 5
  • 15