Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle getResults from multiple activities?

I am trying to get results back from intents in android studio.

In my main I start an activity and use startActivityForResult(intent, 1)

I then use get results in mainActivity from activity 2's setResults()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            Bundle extras = data.getExtras();
            if (extras != null) {
                String name = extras.getString("FIRSTNAME");
                String Lname = extras.getString("LASTNAME");
                int ID = extras.getInt("ID");

                //TODO: Get the list fragment to newinstance with out new arraylist

                Person p = new Person(name, Lname, ID);
                people.add(p);
                getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
            }

In my fragment in activity 1 I am calling a new startActivityForResult(i, 2)

How do i get my main activity to grab the setResults() from Activity 3?

Activity 3 is doing this:

Intent deleteIntent = new Intent();
deleteIntent.putExtra("FNAME", first);
deleteIntent.putExtra("LNAME", last);
deleteIntent.putExtra("ID", num);
setResult(RESULT_OK, deleteIntent);
finish();

I am trying to have my main activity call if (requestCode == 2)

But it works to no avail.

Here is the all the onActivityResult for reference:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            Bundle extras = data.getExtras();
            if (extras != null) {
                String name = extras.getString("FIRSTNAME");
                String Lname = extras.getString("LASTNAME");
                int ID = extras.getInt("ID");

                //TODO: Get the list fragment to newinstance with out new arraylist

                Person p = new Person(name, Lname, ID);
                people.add(p);

                getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
            }

            // NOW SEEING IF THE DETAILS SCREEN PASSED BACK RESULTS

        } else if (requestCode == 2) {
            if (resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                if (extras != null) {
                    String name = extras.getString("FNAME");
                    String Lname = extras.getString("LNAME");
                    int ID = extras.getInt("ID");

                    Person p = new Person(name, Lname, ID);
                    // Delete happens here //
                    if (people.contains(p)) {
                        people.remove(p);
                        // If empty show blank frag, if not, update list //
                        if (people.isEmpty()) {
                            getFragmentManager().beginTransaction().replace(R.id.content_main, BlankList.newInstance());
                        } else {
                            getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
                        }

                    } else {
                        Toast.makeText(this, "DIDNT RECEIVE SAME INFO", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
        // END ELSE CHECK
    }
}

Here is the code that is calling the startActivityForResult() in the Fragment on activity 1.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    //super.onListItemClick(l, v, position, id);

    ArrayList<Person> people = (ArrayList<Person>) getArguments().getSerializable(ARG_People);

    if (people != null && position != -1) {
        Person listPerson = people.get(position);
        Intent i = new Intent("OPENDETAILS");
        i.putExtra("NAME", listPerson.name);
        i.putExtra("LASTNAME", listPerson.lName);
        i.putExtra("ID", listPerson.ID);
        startActivityForResult(i, 2);
    } else {
        Toast.makeText(getActivity(), "EMPTY LIST ERROR", Toast.LENGTH_SHORT).show();
    }
}
like image 617
A. Petrizza Avatar asked Jan 05 '23 22:01

A. Petrizza


1 Answers

It's a little unclear what you're trying to do, but it sounds like:

  1. Activity1 starts Activity2 for result
  2. Activity2 starts Activity3 for result
  3. Activity3 returns a result
  4. Activity1 is expected receive Activity3's result.

If I got that right then the key element that seems to be missing here is that you are expecting Activity1 to get a result from Activity3 even though it was Activity2 that started it for result. In this case you should implement onActivityResult in Activity2, handle the results coming back from Activity3 and set them as Activity2's results to pass back to Activity1 and then finish; An activity will only receive results from activities it directly starts via startActivityForResult.

like image 64
Nick Avatar answered Jan 21 '23 06:01

Nick