Search code examples
androidemulationback-button

Transition between screens on Android


My application has screens and there is only one activity. My main activity is like: I have methods for calling the screen, I didn't use activity (I read somewhere that usage is more complicated.) So I used methods, but there is one problem.

When I open the activity the loginScreen is coming OK

When I click the loginBtn the listScreen is coming OK

But, when I click the back button in listScreen for coming back to loginScreen, the app is closing and open the home screen of emulator.

Normally;

loginScreen --> loginBtn --> listScreen --> listBtn --> writeScreen

What I want is coming back on this path with "back" button on emulator. I don't want to do that with override the back button because with this way I can only set one thing into the method.

public class EmailActivity extends Activity 
{
public void onCreate(Bundle savedInstanceState) 
        {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        loginScreen();
    }

public void loginScreen() 
{
        this.setContentView(R.layout.main);
                Button loginBtn = (Button) findViewById(R.id.btnLogin);

        loginBtn.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) 
                        {
                listScreen();
            }

        });
}

public void listScreen()
{
            this.setContentView(R.layout.list);
                Button listBtn = (Button) findViewById(R.id.btnList);

        listBtn.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) 
                        {
                setContentView(R.layout.writeScreen);
            }

        });

}

Solution

  • Because you must write an Activity for each screen you use in the app... you are just calling methods, and in memory you have just one Activity so, the back button pop the Activity and return to the home... Read this: http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

    If you wanna call another screen(Activity)m you create an instance of a Intent, which tells the system "hey!, call the stuff i'm passing here", for example, you have two activities, called, A1 and A2, if you wanna call A2 from A1, you must do this:

    Intent intent = new Intent(A1.this,A2.class);
    startActivity(intent);
    

    It will push A2 over A1, so when you press back, you will get A1