0

I've this code:

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent(this, ConnectDialog.class);
        update();
    }

    private void update(){
        if(a)
            startActivity(intent);
        else{
            //code
        }
    }

}

And this:

public class ConnectDialog extends Activity{

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect_dialog);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}

The problem is: when I click on the button of the Intent, is it possible to execute the method update of the main activity again? Thank you very much

user3503550
  • 73
  • 1
  • 8
  • "button of the Intent" are you talking about `btn` in `ConnectDialog`? – codeMagic May 21 '14 at 13:42
  • You can use [startActivityForResult()](http://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541) – codeMagic May 21 '14 at 13:50

3 Answers3

1

Just put the call to update into onResume() of the MainActivity. This way, it will be called at first startup and when the MainActivity is shown again later on:

@Override
protected void onResume(){
    super.onResume;
    update();
}
FD_
  • 12,947
  • 4
  • 35
  • 62
0

I would use the onActivityResult method.

Change your update method to

int YOUR_RESULT = 100;

private void update(){
    if(a)
        startActivityForResult(intent, YOUR_RESULT);
    else{
        //code
    }
}

then in that same activity, use this method:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == YOUR_RESULT) {
        update();
    }
}

If you use this method, the update() method will only get called when coming back from that activity.

coder
  • 10,460
  • 17
  • 72
  • 125
0

if u check the Android Life Cycle u will get the answer why the Update is not executing.. How ever to call the update method on click..

 private void update(){
        if(a)
            startActivityForResult(intent, 0);
        else{
            //code
        }
    }

and use onActivityResult to call the update method again

@Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {

        super.onActivityResult(arg0, arg1, arg2);
        update();

    }
Biplab
  • 564
  • 1
  • 5
  • 19