1

We are creating an app with two main views: sView and sViewSettings. If the Android Back button is pressed we want an if statment to check if the current view is set to sView settings, if it is then call the sView.

Already have a listener setup for the back button just need it to call the if statement to check the current view.

Have already tried if (this.findViewById(android.R.id.content) == sViewSettings)

Any ideas on this?

Thank you for Reading, Travis

Travis Elliott
  • 291
  • 2
  • 5
  • 18
  • maybe that will help you http://stackoverflow.com/questions/3736367/why-isnt-there-a-getcontentview-method-for-activity – alaster Feb 06 '12 at 18:19

1 Answers1

3

The view with id android.R.id.content is a FrameLayout holding your content view. Try this:

ViewGroup contentFrame = (ViewGroup) findViewById(android.R.id.content);
if (contentFrame.getChild(0) == sViewSettings) { ... }

However, I suggest a slightly different approach: use a ViewSwitcher (or any kind of ViewAnimator) to flip between the two main views and keep track in your code of which one is on display.

EDIT: If you want to keep your layouts loaded separately, you can assign an id (the same one) to the root view of each layout and then retrieve the content view directly using findViewById.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks that ViewGroup code worked great, not sure exactly how it works but it does. However now I have a new problem. The view switches as it should when the back button is pressed but then it immediately closes the app, any idea how to disable that? – Travis Elliott Feb 06 '12 at 20:53
  • @TravisElliott - It depends on how you are catching the back button. See [this thread](http://stackoverflow.com/questions/7550505/override-android-back-button) and the docs for [`onBackPressed()`](http://developer.android.com/reference/android/app/Activity.html#onBackPressed%28%29) for more info. – Ted Hopp Feb 06 '12 at 21:25