1

I'm new in Android..

I have a question about MasterDetail template.

I have an Activity to manage user login. When the login data are correct I need to show the Master Detail template.

So, in Android Studio, i added a Master/Detail Flow activity in my project.

In my validateLogin() method, after the login data are correct, I have to show Activitys in the Master/Detail flow and I use this code:

Intent myIntent = new Intent(Login.this, MasterDetailListActivity.class);
startActivity(myIntent);
finish();

I'm not secure if this code is correct and I would to show also the actionBar (actually I can see the ActionBar in detailView only).

****** EDIT ********

This is the code of my list activity

public class BusinessPartnerListActivity extends FragmentActivity
        implements BusinessPartnerListFragment.Callbacks {

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_businesspartner_list);

        if (findViewById(R.id.businesspartner_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            ((BusinessPartnerListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.businesspartner_list))
                    .setActivateOnItemClick(true);
        }

        // TODO: If exposing deep links into your app, handle intents here.
    }

    /**
     * Callback method from {@link BusinessPartnerListFragment.Callbacks}
     * indicating that the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(String id) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(BusinessPartnerDetailFragment.ARG_ITEM_ID, id);
            BusinessPartnerDetailFragment fragment = new BusinessPartnerDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.businesspartner_detail_container, fragment)
                    .commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, BusinessPartnerDetailActivity.class);
            detailIntent.putExtra(BusinessPartnerDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }
}

...and detail activity...

public class BusinessPartnerDetailActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_businesspartner_detail);

        // Show the Up button in the action bar.
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // savedInstanceState is non-null when there is fragment state
        // saved from previous configurations of this activity
        // (e.g. when rotating the screen from portrait to landscape).
        // In this case, the fragment will automatically be re-added
        // to its container so we don't need to manually add it.
        // For more information, see the Fragments API guide at:
        //
        // http://developer.android.com/guide/components/fragments.html
        //
        if (savedInstanceState == null) {
            // Create the detail fragment and add it to the activity
            // using a fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(BusinessPartnerDetailFragment.ARG_ITEM_ID,
                    getIntent().getStringExtra(BusinessPartnerDetailFragment.ARG_ITEM_ID));
            BusinessPartnerDetailFragment fragment = new BusinessPartnerDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.businesspartner_detail_container, fragment)
                    .commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpTo(this, new Intent(this, BusinessPartnerListActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

***** EDIT (Video) ****** Video of issue on ActionBar animation

https://www.dropbox.com/s/af07ovbv36pn44x/actionbab_issue.mov?dl=0

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
Safari
  • 11,437
  • 24
  • 91
  • 191

2 Answers2

3

You need to extend from ActionBarActivity instead of FragmentActivity. Change your listview activity definition to:

public class BusinessPartnerListActivity extends ActionBarActivity
    implements BusinessPartnerListFragment.Callbacks {


    // rest of the code remains the same ...
    // ...
    // ...

}

Try this. This will work.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • OK, now I can see the actionBar also on list activity... But,why I see a strange animation when I select an item in the list before visualize the detail? The ActionBar does NOT remain fixed (changing only the content (master-> detail) ... I see a quick animation: it is as if the ActionBar went down slightly before displaying the detail. Can you tell me why is that? You can avoid this one? – Safari Feb 19 '15 at 12:09
  • @Safari: can you provide a picture or video of what exactly happens when you select a list item ? Also please provide your code for detail activity – Yash Sampat Feb 19 '15 at 12:22
  • @Zygotelnit I edited the question: I added the code and video of Action bar animation. – Safari Feb 19 '15 at 12:41
  • @Safari: This seems to be a problem with the new Lollipop animation transitions. Take a look at [this](http://stackoverflow.com/questions/26567822/hiccups-in-activity-transitions-with-shared-elements) post, might be useful :) – Yash Sampat Feb 19 '15 at 13:14
0

Yes your are doing it right, i mean your code is perfect. If you want to finish your current then

finish();

is perfect other wise no need to call it.

BitLord
  • 9
  • 7
  • Ok, thanks... and How can I show the ActionBar in the List (Master) activity? (actually I can see the ActionBar in detailView only). – Safari Feb 19 '15 at 11:18
  • @ZygoteInit I edited my question with the code of my list activity – Safari Feb 19 '15 at 11:50