Fragments and Notifications: Target different Activities from Notification; depending on screen configuration

前端 未结 3 1517
攒了一身酷
攒了一身酷 2021-02-07 21:28

Question:

How to decide what Activity a Notification should launch if the target might depend on the configuration (screen size, orientation

3条回答
  •  攒了一身酷
    2021-02-07 22:05

    When I'm using a dual pane/single pane approach in my apps then I'm only using one activity. In this case it means get rid of ArticleActivity. Here's how you could proceed instead:

    First off you control the appearance of the fragments by using XML layouts for the different configurations, e.g:

    single pane (res/layout):

    
    
    
            
    
    
    

    dual pane (res/layout-xlarge-land):

    
    
    
            
    
            
    
    
    

    In NewsReaderActivity you only need to check which fragments are existing in the layout:

    boolean isMainFragment = (findViewById(R.id.mainFragment) != null);
    if (isMainFragment) {
        mainFragment = new ListFragment();
    }
    
    boolean isDetailFragment = (findViewById(R.id.detailsFragment) != null);
    if (isDetailFragment) {
        detailFragment = new DetailsFragment();
    }
    
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (isMainFragment ) {
        transaction.add(R.id.mainFragment, mainFragment);
    }
    if (isDetailFragment ) {
        transaction.add(R.id.detailFragment, detaislFragment);
    }  
    transaction.commit();
    

    Then when you are in single pane mode (detailsFragment not existing) and want to show the details screen then you simply launch the same activity again but include a parameter in the intent to tell which content is needed:

    void onViewDetails() {
        Intent i = new Intent(this, NewsReaderActivity.class);
        i.putExtra("showDetails", true);
        startActivity(i);
    }
    

    In onCreate() of your activity you choose the fragment depending on this parameter:

    boolean isDetailFragment = (findViewById(R.id.detailsFragment) != null);
    if (!isDetailFragment) {
        boolean showDetails = getIntent().getBooleanExtra("showDetails", false);
        if (showDetails) {
           mainFragment = new DetailsFragment();
        }
        else {
           mainFragment = new ListFragment();
        }
    }
    

    Now when you finally launch the activity from the notification then you just don't care about the current screen orientation anymore!

    Just include the "showDetails" flag in your intent and set it as appropriate, just like it's done in onViewDetails(). Your activity will then show both fragments when you are in dual pane mode (you can still do some special behavior if "showDetails" is true), or otherwise when you are in a configuration that requires single pain mode, then the fragment you specified in the "showDetails" flag is shown.

    I hope this helps and gives you a good understanding about this approach.

提交回复
热议问题