Home:ALL Converter>Call New Intent(Actvity) from Fragment retaining left navigation

Call New Intent(Actvity) from Fragment retaining left navigation

Ask Time:2014-05-15T14:31:44         Author:user3639304

Json Formatter

Call New Intent(Actvity) from Fragment retaining left navigation

I have used navDrawer as LeftMenu in my android application which renders the fragemnts on the right content. I have refered the link (https://github.com/jamesmontemagno/Xam.NavDrawer/blob/master/Sample/NavDrawer/Activities/HomeActivity.cs) for navDrawer.

The scneario is when application starts the navigation bar dispalys 2 fragemtns in the right content. Each fragment is associated with the menu option and the menu option is dispalyed in the right corner at the top.

item.xml

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
  android:id="@+id/RiskProfile"
  android:title="@string/RiskProfile"
/>

<item
  android:id="@+id/PlaceOrder"
  android:title="@string/PlaceOrder"
/>

<item
  android:id="@+id/FixAppointment"
  android:title="@string/FixAppointment"/>

<item
  android:id="@+id/AddOpportunity"
  android:title="@string/AddOpportunity"
/>
</menu>

itemone.xml

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
  android:id="@+id/Computer"
  android:title="@string/Computer"
/>
<item
  android:id="@+id/Phone"
  android:title="@string/Phone"
 />
<item
    android:id="@+id/PlaceOrder"
    android:title="@string/PlaceOrder"
/>
</menu>

Fragment A uses item.xml.

public override void OnCreateOptionsMenu(IMenu menu, MenuInflater menuInflater)
{
        menuInflater.Inflate(Resource.Menu.items, menu);
        base.OnCreateOptionsMenu(menu, menuInflater);
}

Fragment B uses itemone.Xml.

public override void OnCreateOptionsMenu(IMenu menu, MenuInflater menuInflater)
{
        menuInflater.Inflate(Resource.Menu.itemone, menu);
        base.OnCreateOptionsMenu(menu, menuInflater);
}

Fragment A uses item.xml. Fragment B uses itemone.Xml. Both these fragment's have duplicate menu item (PlaceOrder), so when both these fragments are loaded in the right content of navigation drawer, the right content displays the single option menu at the right top in such a way that it displays the duplicate item once.

Below is the code for each fragment which displays the single menu option at the right top corner.

public override void OnPrepareOptionsMenu(IMenu menu)
{
        for (int i = 0; i < menu.Size(); i++)
        {
            for (int j = i + 1; j < menu.Size(); j++)
            {
                if (menu.GetItem(i).ItemId == menu.GetItem(j).ItemId)
                { menu.GetItem(i).SetVisible(false); }
            }
        }
}

Fragment A OnOptionsItemSelected.

public override bool OnOptionsItemSelected(IMenuItem item)
{
        base.OnOptionsItemSelected(item);


        switch (item.ItemId)
        {
            case  Resource.Id.AddOpportunity:
                // Toast .MakeText(this.View, "You selected Phone" , ToastLength.Short).Show();
                break;

            case Resource.Id.FixAppointment:
                // Toast.MakeText(BaseContext, "You selected Computer",   ToastLength.Short).Show   ();
                break;

            case Resource.Id.PlaceOrder:
                activity = new Activity();
                try
                {
                    var intent = new Intent(activity, typeof(PlaceOrderView));
                    activity.StartActivity(intent);
                }
                catch(Exception e)
                {
                    throw new Exception();
                }
                //StartActivity(typeof(PlaceOrderView));

               // AndroidAppApplication.Instance.Initialize(ActivityView.PlaceOrderView, null);
                //var view = LayoutInflater.Inflate(Resource.Layout.LoginView, null, false);
                //this.Initialize(view, AndroidAppApplication.Instance.LoginViewModel);
                // Toast.MakeText(BaseContext, "You selected Gamepad", ToastLength.Short).Show();
                break;

            case Resource.Id.RiskProfile:
                // Toast.MakeText(BaseContext, "You selected Camera", ToastLength.Short).Show();
                break;
        }

        return base.OnOptionsItemSelected(item);
    }

Fragment B OnOptionsItemSelected.

public override bool OnOptionsItemSelected(IMenuItem item)
    {
        base.OnOptionsItemSelected(item);

        switch (item.ItemId)
        {
            case Resource.Id.Computer :
                // Toast .MakeText(this.View, "You selected Phone" , ToastLength.Short).Show();
                break;

            case Resource.Id.Phone:
                // Toast.MakeText(BaseContext, "You selected Computer", ToastLength.Short).Show();
                break;

            case Resource.Id.PlaceOrder:
                activity = new Activity();
                 var intent = new Intent(activity, typeof(PlaceOrderView));
                activity.StartActivity(intent);
                // Toast.MakeText(BaseContext, "You selected Gamepad", ToastLength.Short).Show();
                break;
        }

        return base.OnOptionsItemSelected(item);
    }

So the questions are:

1) When I select the Option Menu item the application life cycle calls both the fragment OnOptionsItemSelected event. Is this the correct behavior of android Application?

2) When I select the Place Order item it should display a new activity screen unloading the fragment and should also have the capability of retaining the left navigation bar. Is it possible?

Author:user3639304,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/23670828/call-new-intentactvity-from-fragment-retaining-left-navigation
yy