Travis Elliott
Travis Elliott

Reputation: 291

Getting Currently Set Content View

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

Upvotes: 1

Views: 2246

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234817

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.

Upvotes: 3

Related Questions