-2

Recently I have been working on an Android app in which the Main activity has 5 buttons all of them would direct to new activities.

Now the problem I'm facing is that when I click say 2nd button then the 5th activity opens then on pressing back button 4th activity opens then 3rd and then 2nd activity. I could not figure out the problem.

code :

package com.example.lenovo.meme_generator_new;

    import android.content.Context;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;

    public class MainIKActivity extends AppCompatActivity implements 
    View.OnClickListener {
    Button button1,button2,button3,button4,button5,change;
    ImageView imageView1,imageView2,imageView3,imageView4,imageView5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_ik);
    init();
    }
    public void init()
    {
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    button5 = (Button) findViewById(R.id.button5);
    imageView1 = (ImageView) findViewById(R.id.imageView1);
    imageView2 = (ImageView) findViewById(R.id.imageView2);
    imageView3 = (ImageView) findViewById(R.id.imageView3);
    imageView4 = (ImageView) findViewById(R.id.imageView4);
    imageView5 = (ImageView) findViewById(R.id.imageView5);
    change= (Button) findViewById(R.id.change);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    button4.setOnClickListener(this);
    button5.setOnClickListener(this);
    change.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch(view.getId()) {
        case  R.id.button1:
            Intent intent1 = new Intent(MainIKActivity.this,ik1.class);
            startActivity(intent1);
            break;
       case  R.id.button2 :
            Intent intent2 = new Intent(MainIKActivity.this,ik2.class);
            startActivity(intent2);
       case  R.id.button3 :
            Intent intent3 = new Intent(MainIKActivity.this,ik3.class);
            startActivity(intent3);
        case  R.id.button4 :
            Intent intent4 = new Intent(MainIKActivity.this,ik4.class);
            startActivity(intent4);
        case  R.id.button5 :
            Intent intent5 = new Intent(MainIKActivity.this,ik5.class);
            startActivity(intent5);
            break;
        case  R.id.change :
            Intent intentc = new Intent(MainIKActivity.this,MainOMActivity.class);
            startActivity(intentc);
    }

    }

}

2 Answers2

0
                        button1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(ThisActivity.this,NextActivity1.class);
                                startActivity(intent);
                            }
                        });
                        button2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(ThisActivity.this,NextActivity2.class);
                                startActivity(intent);
                            }
                        });
                        button3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(ThisActivity.this,NextActivity3.class);
                                startActivity(intent);
                            }
                        });
                        button4.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(ThisActivity.this,NextActivity4.class);
                                startActivity(intent);
                            }
                        });
                        button5.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(ThisActivity.this,NextActivity5.class);
                                startActivity(intent);
                            }
                        });

In each your Next Activity add this Override method

@Override
public void onBackPressed() {
   finish();
}
Ramaraju
  • 604
  • 1
  • 6
  • 17
0

Use the below code:

Intent intent = new Intent(this, A.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); /

The above code will close all other activity on top of main thread. And make sure that your closing all the break statements .

Divakar R
  • 773
  • 1
  • 8
  • 36