Teererai Marange
Teererai Marange

Reputation: 2132

Android switching activities

I have 2 activities. One called Login and another called ListDeals. Login has a form with a submit button which, when pressed, should switch our activity to ListDeals. This works.

However, in Logcat I don't get a message saying that the activity has started. Also, when in ListDeals, if the user presses the back button, both activities should be killed. I looked around and this is what I came up with:

public class Login extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_screen);

    Button submit=(Button)findViewById(R.id.submit);
    submit.setOnClickListener(onSubmit);
}

private View.OnClickListener onSubmit=new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        Intent myIntent=new Intent(view.getContext(),ListDeals.class );
        startActivityForResult(myIntent,0);

    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.login_screen, menu);
    return true;
}

@Override
public void onActivityResult(int requestCode,int resultCode, Intent data) {
    if(resultCode==2) {
        finish();
    } 
}

}

public class ListDeals extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deals_list);
        System.out.println("starting activity");
    }

    @Override
    protected void onDestroy() {
        System.out.println("dude");
        setResult(2);
        super.onDestroy();


    }

    protected void onStop(){
        setResult(2);
        super.onStop();

    }

}

However, when I press the back button, It takes me back to the Login activity which is not what I want.

Upvotes: 5

Views: 7513

Answers (2)

wsanville
wsanville

Reputation: 37516

In your click handler, onSubmit, just finish your login activity, like this. That way, it won't be on the activity stack, and as a result, pressing back from your other activity will bring the user back to where they started.

private View.OnClickListener onSubmit=new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        Intent myIntent=new Intent(view.getContext(),ListDeals.class);
        startActivity(myIntent);
        finish();
    }
};

Also, to address you second question, use the Log class methods, such as Log.i or Log.d for debug messages, those will show up in logcat.

Upvotes: 5

davidcesarino
davidcesarino

Reputation: 16228

Finish Login before starting the new Activity. Besides you don't need the result.

private View.OnClickListener onSubmit=new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        Intent myIntent=new Intent(Login.this, ListDeals.class );
        startActivity(myIntent);
        Login.this.finish() // add this to finish it.

    }
};

Upvotes: 1

Related Questions