0

possible duplicate of How to close activity and go back to previous activity in android

The problem is, I add finish() at the end of the method I'm currently on, and the whole app closes.

I want it to return back to the previous screen by pressing the back button on the phone (I don't want to add a back button in the app)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void secondScreen(View v) {
        setContentView(R.layout.activity_second_screen);
    }

I want to return to main from secondScreen()

Community
  • 1
  • 1
Adz
  • 2,809
  • 10
  • 43
  • 61
  • I am not sure what are you trying to do here. You have a setContentView method set to activity_main(which I assume is your launching activity). In addition to that, you have method secondScreen and you have called a setContentView set to layout of second activity. Can you please post a complete source code(both activities) for more clarity? – Harsh Singal Nov 27 '13 at 01:48
  • Acitity_main is just a screen with a Image button, and activity_second_screen just has three normals buttons in it. Do you still need the source code for them? – Adz Nov 27 '13 at 01:54
  • So in a single activity, you are switching to a second activity by just changing the contentView which activates when you click on the Image button. Am I right? The main purpose of my asking for the source code is to see how are you going from main activity to second activity – Harsh Singal Nov 27 '13 at 01:58
  • Yes, you are correct. – Adz Nov 27 '13 at 01:58
  • Then your implementation is not a possible duplication of the link you provided in your post, as that example has two different activities. Please refer to codeMagic's reply below. – Harsh Singal Nov 27 '13 at 02:03

1 Answers1

3

It looks like you only have one Activity and you are just changing the layout with setContentView(). While you could fix this by overriding onBackPressed() and changing the layout there, this is not recommended. If you want to separate layouts then you should have two separate Activities. So you should create a second Activity as you did with the first and in the onCreate() you would have setContentView(R.layout.activity_second_screen);

Then in your, I'm guessing it is, onClick() you would use an Intent to go to that second Activity.

public void secondScreen(View v) {
    Intenet i = new Intent(v.getContext(), NextActivityName.class);
    startActivity(i);
}

Activities

Intents

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Hi, thanks for your help. I'm getting an error when I click on activity_main image button. Here is my code: http://pastebin.com/qbMwdnKx – Adz Nov 27 '13 at 03:03
  • That doesn't appear to be all of your logcat – codeMagic Nov 27 '13 at 03:12
  • Just as it says, you need to declare it in your `manifest.xml` as you have your first `Activity`. [Manifest Docs](http://developer.android.com/guide/topics/manifest/manifest-element.html) – codeMagic Nov 27 '13 at 03:19