Question

I have 2 activities. In my first, i have a button and when i click on it, it start the second activity . But when i go back to my first and i click for second time my button, my second activity start but i have to go back two time to go back in my firts activity.

If a click one again a will have to go back 3 time ...

help me please. and thank you in advance :)

this is my code:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this._that = this;
    _progressDialog = new ProgressDialog(this);

   EditText editText = (EditText) findViewById(R.id.MainActivityEditText);
   editText.setText("T_F81D4FA3F8");

   Button button = (Button) findViewById(R.id.MainActivityButton);
   button.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.MainActivityButton ) {
        Log.w("MainActivity", "onClick");
        _progressDialog.setMessage("Chargement en cours");
        _progressDialog.show();

        new Thread(new Runnable() {
            public void run() {
                EditText editText = (EditText) findViewById(R.id.MainActivityEditText);
                String s = editText.getText().toString().replace(" ", "");
                Log.i("EditText", s);
                ID_APPLICATION = s;
                //if (! Datas.getInstance().isUpdateDatas())
                    WebService.getInstance().datas(_that);
                LocalBroadcastManager.getInstance(_that).registerReceiver(datasUpdateFail, new IntentFilter("datas-update-fail"));
                LocalBroadcastManager.getInstance(_that).registerReceiver(datasUpdate, new IntentFilter("datas-update"));
            }
        }).start();


    }
}


BroadcastReceiver datasUpdate = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          myStartActivity("ACCUEIL");
          _progressDialog.dismiss();
      }
};
BroadcastReceiver datasUpdateFail = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          Toast.makeText(MainActivity.this, "Erreur : Veuillez verifier votre identifiant ou votre connexion", Toast.LENGTH_SHORT).show();
          _progressDialog.dismiss();
      }
};

public void myStartActivity(String page){
    Intent intent = new Intent(this, PageActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("page", page);
    intent.putExtras(bundle);
    this.startActivity(intent);
}
Was it helpful?

Solution

unregister the receiver

 @Override
protected void onPause() {
    // Unregister receiver
    LocalBroadcastManager.getInstance(this).unregisterReceiver(datasUpdate);
    LocalBroadcastManager.getInstance(this).unregisterReceiver(datasUpdateFail);
    super.onPause();
}

OTHER TIPS

Every time you get into this activity you register a received. So the second time you come back, you have 2 receivers registered. Therefore when you click on button 2 activities will be opened.

Try uninteresting the regiseter when you are done with it. eg. Before starting the new activity.

 LocalBroadcastManager.getInstance(_that).unregisterReceiver(datasUpdateFail);
 LocalBroadcastManager.getInstance(_that).unregisterReceiver(datasUpdate);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top