Aditi K
Aditi K

Reputation: 1554

how to get value to parent activity from its child activity and also retain them in android?

I am calling child activity from parent activity my child activity is list view & parent contains data from list view Such as selected list item Name and ID in button text on parent

A->B->C and c is list view sending data back to Parent B after closing using Intent and using

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

to make only single activity instance at a time but my problem is I am using button click to call list view activity and on selection i get value back to parent which work fine in case of single button click but if i use multiple buttons to call tht list view with different data each time it does not retain previous selected data to parent activity it just get wipe out

parent & List view child

how to retain tht different values on each button click

ParentActivity.java

btnCust = (Button) findViewById(R.id.btnCust);      
    btnCust.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            String flag ="CU";
            //Intent intent = new Intent(getBaseContext(),MyListActivity.class);
            //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            //intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            //intent
            Intent i = new Intent(getBaseContext(),MyListActivity.class);
            i.putExtra("flag", flag);
            startActivityForResult(i, 1);
         }
    });

    btnPospect = (Button) findViewById(R.id.btnPospect);        
    btnPospect.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            String flag ="P";
            //Intent intent = new Intent(getBaseContext(),MyListActivity.class);
            //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            //intent.putExtra("flag", flag);
            /*Intent i = new Intent(getBaseContext(), MyListActivity.class);
            i.putExtra("flag", flag);
            startActivityForResult(i, 1);
            startActivity(i);*/
            Intent i = new Intent(getBaseContext(),MyListActivity.class);
            i.putExtra("flag", flag);
            startActivityForResult(i, 2);

        }
    }); 
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    name = data.getStringExtra("nm");
    if(requestCode==1 && resultCode==RESULT_OK){
        btnCust.setText(name);
    }
     if(requestCode==2 && resultCode==RESULT_OK) {          
         btnPospect.setText(name);
    }
 }

in child listview.java

if (flag.equals("CU")) {
        custList = db.getAllCustomers();
        cl.setCustLst(custList);
    } else if (flag.equals("P")) {
        prosList = db.getAllProspects();
        cl.setProsLst(prosList);
    }
    final CustomListenerAdapter customAdapter = new CustomListenerAdapter(this, cl);
    list.setAdapter(customAdapter);

    if (flag.equals("CU")) {
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> myAdapter, View myView,int myItemInt, long mylng) {

                Customer cust = (Customer) customAdapter.getItem(myItemInt);
                id = cust.getId();
                nm = cust.getNm();

                close();
            }
        });

    }else if (flag.equals("P")) {
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> myAdapter, View myView,
                    int myItemInt, long mylng) {

                Prospect pros = (Prospect) customAdapter.getItem(myItemInt);
                id = pros.getId();
                nm = pros.getNm();

                close();
            }
        });
    }

public void close() {
 Intent intent=new Intent();
     intent.putExtra("flag",flag);
     intent.putExtra("id", id);
     intent.putExtra("nm", nm);
      setResult(RESULT_OK,intent);
        finish();

}

Upvotes: 3

Views: 1811

Answers (2)

Abhishek Agarwal
Abhishek Agarwal

Reputation: 1897

In parent activity

 btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivityForResult(new Intent(ParentActivity.this,ChildActivity.class),1);

        }
    });

   btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivityForResult(new Intent(ParentActivity.this,ChildActivity.class),2);

        }
    });

onActivityResultMethod() in parent activity

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==1 && resultCode==RESULT_OK)
  {
    btn1.setText(data.getStringExtra("text"));
    }

     if(requestCode==2 && resultCode==RESULT_OK)
   {
    btn2.setText(data.getStringExtra("text"));
    }


 }

In Child Activity

         list.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {
                Intent intent=new Intent();
 intent.putExtra("text",your_text_to_send_back);
  setResult(RESULT_OK,i);
    finish();
                }
            });

You have problem with your settings in mobile. Uncheck the Don't keep activities in Developers Option Settings. It will work fine.

Upvotes: 4

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

Actually you can keep the data outside these activity classes

If your data not more heavy you can use a singleton pattern. Then the data is accessible from all the classes.

Upvotes: 2

Related Questions