简体   繁体   中英

How to start new activity on button click in android?

I am trying to go from one activity to another when clicking the 'Login' button, here is my code for button click-

public void onClick(View v) {
                // TODO Auto-generated method stub

                if(id.getText().toString().equals("ajay")&&pass.getText().toString().equals("sainy"))
                {
                    Intent i = new Intent(MainActivity.this,adminhome.class);
                    MainActivity.this.startActivity(i);

                }
                else if(id.getText().toString().equals("aj")&&pass.getText().toString().equals("sa"))
                {
                    Intent i = new Intent(MainActivity.this,userhome.class);
                    MainActivity.this.startActivity(i);                 
                }
                else
                    res.setText("Incorrect Credentials...Retry");
            }
        });

but when I enter correct credentials 'adminhome' or 'userhome' activity is not starting. The same 'MainActivity' is opening again. I think I have problem in following code -

Intent i = new Intent(MainActivity.this,adminhome.class);
                    MainActivity.this.startActivity(i);

or in,

Intent i = new Intent(MainActivity.this,userhome.class);
                    MainActivity.this.startActivity(i);                 

What is the problem? Please help, I am learning android.

The answer is in the comments, but anyway, when anyone of you encounters such behavior:

  • Make sure the other activities do not have the same layout, and, so, you will think that the same activity is being open when it is the other activity. Just double check the layout of the activities.

you can check and see if the new activity is put inside the manifest or not, as i did below

        <activity
        android:name="com.hossa.multitask.activity2"
        android:label="@string/app_name" >
        </activity>

Implement the View.OnClickListener interface and override the onClick method.

ImageView btnWatchVideo;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search1);
        ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch);
        btnSearch.setOnClickListener(this);
    }
 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSearch: {
                Intent intent = new 
             Intent(Search.this,SearchFeedActivity.class);
                startActivity(intent);
                break;
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM