Namas
Namas

Reputation: 37

get current activity view after setContentView

i'm using a listview with an onclickitemlistener when i'm clicking on an item of the listview it change the view with setContentView ( from view_list.xml to view_detail.xml)

how can i know what is my current view of my activity (list or detail).

in my onBackPressed() i want to switch to the view_list if i'm in the view_detail if i'm already in my view_list layout i want to finish() the activity

public void onBackPressed() {
    if ( ?????? !== findViewById(R.id.view_list_id));
          setContentView(R.layout.view_list); 
    else
          finish();
    }

i could use an activity instead of just changing layout but i feel like it could work this way.

thanks.

Upvotes: 0

Views: 866

Answers (3)

amalBit
amalBit

Reputation: 12181

A better approach would be a Master/Detail flow:

You can easily acheive it if you are using Eclipse with ADT. Just create a new project with master detail Template.

Some helpful tutorials regarding this:

Upvotes: 0

FD_
FD_

Reputation: 12919

Don't change the content view of an Activity.

If you want to have a List and Detail page, use Fragments instead. This will also allow you to display the list as a side pane on tablets.

Upvotes: 1

Triode
Triode

Reputation: 11357

if ( null == findViewById(R.id.view_list_id));
    setContentView(R.layout.view_list); 
else{
    finish();
}

Hope this is what you want. This will check if the view contains a view with id view_list_id if so it will finish the activity otherwise replace the view. As the suggestion says this way is not recommended.

Upvotes: 1

Related Questions