-4

There is 2 activities in my project. I want to call a new activity when press the back button. I tried both onBackPressed(),onKeyDown() for that. My code is given below. I searched for this in google but nothing worked for me.

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Log.d("update", is_updated.toString());
        if(is_updated){
            Intent i = new Intent(ProductDetailsActivity.this,ProductListActivity.class);
            startActivity(i);
            finish();
            is_updated = false;
        }else {
            finish();
        }
    }

I logged the value "is_update", even the value is "true" Its not calling the ProductListActivity.class, The same code is working for button click.How I can call the new activity in onBackpressed(), Can anyone help me.

Remees M Syde
  • 2,564
  • 1
  • 19
  • 42

1 Answers1

1

as @Raghunandan said. Here I am explaining his words. If you want to update data in previous activity you should use startActivityForResult for this you call finish() and override finish() and use setResult(RESULT_OK) like:

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Log.d("update", is_updated.toString());
        if(is_updated){

            finish();
            is_updated = false;
        }
    }

@Override
    public void finish() {
        super.finish();

       Intent intent = new Intent();
       intent.putExtra("updated_data", "data");//set whatever your updated data is
       setResult(RESULT_OK, intent);
    }

Now back to the previous activity override onActivityResult method. I suppose you are aware of onActivityResult method and its usage, if not read it from here

Hope you will get the desired results.

XtreemDeveloper
  • 1,132
  • 9
  • 14
  • I dont want the data from that activity, If any update in the second activity(to server), I want to restart the 1st activity on back button press. – Remees M Syde Sep 23 '14 at 07:47
  • But i mentioned,I want to start a new activity in onbackpressed() – Remees M Syde Sep 23 '14 at 07:53
  • `start a new activity in onbackpressed()`..Where is server mentioned in this?? Clear your question first... – XtreemDeveloper Sep 23 '14 at 07:56
  • As i mentioned in the question I just want to restart an Activity when press back button. There is nothing related to server. I want to restart or start an activity when i press back button. – Remees M Syde Sep 23 '14 at 08:00