开发者

How to correct fragment not launching issue?

i am launching a activity hosting a fragment with a list of items..

When an item is clicked. The layout is tested for threePane or not.

    public void onFeedSelected(Feed feed) {
    if (isThreePane) {
        addItemsFragment(feed);
    }
    else {
        Intent i=new Intent(this, ItemsActivity.class);

        i.putExtra(ItemsActivity.EXTRA_FEED_KEY, feed.getKey());

        startActivity(i);
    }
}

As you can see if it is not three pane then it launches an activity..This works fine on regular phones.

But if it is a tablet screen the activity isnt launched in the fragment.

Here is my AddFeed() method.

public void addItemsFragment(Feed feed) {
    FragmentManager fragMgr=getSupportFragmentManager();
    ItemsFragment items=(ItemsFragment)fragMgr.findFragmentById(R.id.second_pane);
    FragmentTransaction xaction=fragMgr.beginTransaction();

    if (items==null) {
        items=new ItemsFragment(true);
        items.setOnItemListener(this);

        xaction
            .add(R.id.second_pane, items)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .addToBackStack(null)
            .commit();
    }
    else {
        ContentFragment content=
            (ContentFragment)fragMgr.findFragmentByI开发者_开发百科d(R.id.third_pane);

        if (content!=null) {
            xaction.remove(content).commit();
            fragMgr.popBackStack();
        }
    }

I dont understand why its not being launched..When i click the item it does nothing on a tablet device.

EDIT:

private boolean isThreePane=false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    FeedsFragment feeds
        =(FeedsFragment)getSupportFragmentManager()
                                                    .findFragmentById(R.id.feeds);

    feeds.setOnFeedListener(this);

    isThreePane=(null!=findViewById(R.id.second_pane));

    if (isThreePane) {
        feeds.enablePersistentSelection();
    }
}


I am not familiar with the Fragment API at all so this is total shot in the dark...

isThreePane=(null!=findViewById(R.id.second_pane));

is getting set to true on the tablet, so that means findViewById(R.id.second_pane) is not returning null. Do you have this second_pane view hard coded in your xml? How does the app decide whether or not the second_pane view should be present? Is it based on screen size at all?

Should you be using something like this:

fragMgr.findFragmentById(R.id.second_pane);

Instead of findViewById? Again I know nothing about fragments api, sorry if that is silly question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜