Search code examples
androidandroid-activityoncreateonresumeactivity-stack

updating the activity when the child activity close


I have an Activity stack like:

activity A make something

activity A call Activity B (without finish();)

activity B make something

activity B call activity C

activity B finish();

activity C make something

activity C finish();

i would like to update the view of the Acivity A since the modification done by activity C has update the database and maybe i have different things to list in activity A.

if my main activity is like that:

public class MainActivity extends ListActivity {
     private pollDataSource datasource;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        datasource = new pollDataSource(this);
        datasource.open();

        Cursor values = datasource.getAllCategorie();

        String[] categorieColumns =
            {
                MySQLiteHelper.COLUMN_NOME   // Contract class constant containing the word column name

            };


            int[] mWordListItems = { R.id.categoria_label };


        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getApplicationContext(),               // The application's Context object
                R.layout.single_list_item,             // A layout in XML for one row in the ListView
                values,                                // The result from the query
                categorieColumns,                      // A string array of column names in the cursor
                mWordListItems,                        // An integer array of view IDs in the row layout
                0);                                    // Flags (usually none are needed)


        setListAdapter(adapter);
    }


    @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {

          TextView textView = (TextView) v.findViewById(R.id.categoria_label); 
          String text = textView.getText().toString(); 

          Intent myIntent = new Intent(MainActivity.this, domandeDiCategoria.class);
          myIntent.putExtra("categoriaName", text);
          myIntent.putExtra("categoriaId", id);
          datasource.close();
          MainActivity.this.startActivity(myIntent);

        }   




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




}

have I had to write the onResume()? and make it similar to the onCreate? or i could use the same cursor, the same adapter and so on to "update" the view? what is better?


Solution

  • 1) Define in A activity

    public static int UPDATE_A_ACTIVITY  = 1;
    

    2) startActivityForResult(intentB, A.UPDATE_A_ACTIVITY); // start B from A

    3) startActivityForResult(intentC, A.UPDATE_A_ACTIVITY); // start C from B

    4) and when all is ok in C activity finish C activity and notify A activity, that he should update something

    in C activity:

     finish();
     setResult(RESULT_OK);
    

    5) after calling setResult(RESULT_OK); will called next method in A (write this in A activity):

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case UPDATE_A_ACTIVITY:
            if (resultCode == RESULT_OK) {
                // update what u want
            }
                break;
        }
    }