简体   繁体   中英

How to refresh the activity which is not finished in android?

Is there any way to refresh or update the activity in android? for example: I am having two activities Counter1 and Counter2 activity.Here in the two activities there is one text view( tv_count ) and the two buttons( bt_plus and bt_minus ), plus and minus button respectively.when the user clicks on the respective button count(textview value) will get increase & decrease, the count starts from zero now the count=4 ,where I am saving the count values in a model class, starting the second activity without finishing the first activity.

button_next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(getApplicationContext(), Counter2.class);
            startActivity(intent);

        }
    });

Now I am navigating to the second activity where i can further increase the count value from 4,now count=6.

button_back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();

        }
    });

when i click back the current activity will get finish.But the value of the count remains same 4 which should be updated to 6 in Counter1 activity.

As per surfing, they are saying to finish the first activity before moving to second activity. But i don't want to finish the first activity rather to do some operation onResume() or onRestart() and onPause() .

Solution need: To update the value of a count on the first activity without finishing and calling the activity explicitly.

Below are the referral links: link1 link2

This question might be asked already but I want some one to brief me clearly. It will be very helpful,Thanks in Advance..

EDITED : Actually this is just about a single value and single textview actually my requirement is some thing else.. I am developing a restaurant app where there will be many products in list view and in expandable list view which is in a sliding tabs. when the user add pizza the count will get increase when the user search the same product in search view the count can be increased.. the increased count is updated in model class but in the list view of sliding tabs is not getting updated.

You can write onActivityResult method in Counter2 Activity and pass the value to Counter1 .

In your Counter1 :

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 // TODO Auto-generated method stub
 super.onActivityResult(requestCode, resultCode, data);
 if(data.getExtras().containsKey("value")){
   txtview.setText(data.getStringExtra("value"));
  }
  }

In your Counter2 :

 i.putExtra("value", value);
 setResult(RESULT_OK, i);

In order to send data between activities, you should start a new Activity via startActivityForResult()

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1000);

If you want to send back data from the SecondActivity

button_back.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent returnIntent = new Intent();
        returnIntent.putExtra("result", result);
        setResult(RESULT_OK, returnIntent);
        finish();

    }
});

And in your FirstActivity.class

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

    if (requestCode == 1000) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
            // Update your view and model with the returned value 
        }
    }
}

You can send the count value from the Counter1 activity to the Counter2 activity. Increase it there and save it there. And while finishing the 2nd activity, send the the updated counter value back to the Counter1 activity.

You can try something like this -

button_next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(getApplicationContext(), Counter2.class);
            intent.putExtra("CURRENT_COUNTER_VALUE", counter);
            startActivity(intent);

        }
});

In the Counter2 activity, you can retrieve the counter value like this -

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String counter = extras.getIntExtra("CURRENT_COUNTER_VALUE",0);

}

Then while finishing the Counter2 activity -

button_back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getApplicationContext(), Counter1.class);
            intent.putExtra("CURRENT_COUNTER_VALUE", counter);
            startActivity(intent);
            finish();
        }
    });

And retrieve the same way in Counter1 activity's onResume() method.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM