0

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.

Community
  • 1
  • 1
Asif Sb
  • 785
  • 9
  • 38
  • i think the problem here can be that you are instantiating model class in both the activities if i am not wrong . Is that so ? – Satyen Udeshi Oct 07 '15 at 05:26
  • You said you are saving the value in model class?? how are you sending the values to Counter2 activity? generally you can send values to another activity through arguments.. but i cannot see that in your code.. so is your class static? – Tarun Oct 07 '15 at 05:27
  • That is just an example.I have not posted my actual code which is too messy.where i have simplified it here with the counter,and no I am not instantiating on both. – Asif Sb Oct 07 '15 at 05:28
  • @AsifSb have to tried to set value of your textview in onResume method? – Tarun Oct 07 '15 at 05:32
  • whenever i am making changes in the count value ill do model.setCount(4); – Asif Sb Oct 07 '15 at 05:33
  • calll model.setCount(4) will set the value of variable in your model class.. but you will need to update the textview... for this you can use onActivityResult method.. or create a interface with a method which is called whenever the values is changed.. implement that interface in your counter1 activity class and update the textiview in the interface method... but as of your current requirement onActivityResult will work perfectly.. – Tarun Oct 07 '15 at 05:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91549/discussion-between-tarun-and-asif-sb). – Tarun Oct 07 '15 at 05:44

3 Answers3

1

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);
Jas
  • 3,207
  • 2
  • 15
  • 45
  • You can pass your count to first activity and use however you want.Or else you can save the count in `SharedPreferences` and retrieve wherever required.This is the best solution since it can be accessed and edited anywhere – Jas Oct 07 '15 at 06:08
0

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 
        }
    }
}
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
0

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.