Alex
Alex

Reputation: 73

Returning from an Activity started by a Fragment

I have an application that uses Tabs. I used Fragments to implement the TabListeners. One of these Fragment starts 2 activities (depending on some buttons).

I used the Eclipse interface to create these activities, meaning that Eclipse took care of all the work (creating the layout, updating the manifest etc.).

I have no issue if I return from the activity using the return button on the phone. But if I use the "<" symbol on the upper left corner of activity layout, I have an error:

01-04 02:56:56.000 E/Activity( 7556): getParentActivityIntent: bad parentActivityName 'com.android.nfcinfo2.FragmentCeSupport' in manifest

01-04 02:56:56.007 E/NavUtils( 7556): getParentActivityIntent: bad parentActivityName 'com.android.nfcinfo2.FragmentCeSupport' in manifest

01-04 02:56:56.007 D/AndroidRuntime( 7556): Shutting down VM**

01-04 02:56:56.007 W/dalvikvm( 7556): threadid=1: thread exiting with uncaught exception (group=0x41c78b90)

01-04 02:56:56.007 E/AndroidRuntime( 7556): FATAL EXCEPTION: main

01-04 02:56:56.007 E/AndroidRuntime( 7556): Process: com.android.nfcinfo2, PID: 7556

01-04 02:56:56.007 E/AndroidRuntime( 7556): java.lang.IllegalArgumentException: Activity EvtTransactionActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data>  element in your manifest?)

01-04 02:56:56.007 E/AndroidRuntime( 7556):     at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:177)

But it looks like my manifest is OK (FragmentCeSupport is the parent, EvtTransaction and SeRouting are the child activities):

  <application
        android:allowBackup="true"
        android:icon="@drawable/stnfcinfo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.android.nfcinfo2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.android.nfcinfo2.EvtTransactionActivity"
            android:label="@string/title_activity_evt_transaction"
            android:parentActivityName="com.android.nfcinfo2.FragmentCeSupport" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.android.nfcinfo2.FragmentCeSupport" />
        </activity>
        <activity
            android:name="com.android.nfcinfo2.SeRouting"
            android:label="@string/title_activity_se_routing"
            android:parentActivityName="com.android.nfcinfo2.FragmentCeSupport" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.android.nfcinfo2.FragmentCeSupport" />
        </activity>
    </application>

Here is how I start the activities from the FragmentCeSupport:

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
    case R.id.evt_tx_button:
    {
        Intent intent = new Intent(getActivity(), EvtTransactionActivity.class);
        startActivity(intent);
    }
        break;

    case R.id.routing_host_button:
    {
        Intent intent = new Intent(getActivity(), SeRouting.class);
        startActivity(intent);
    }
        break;
    }
}

And here is the return code (generated by Eclipse) in EvtTransactionActivity:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case 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.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Upvotes: 3

Views: 5539

Answers (2)

Dhina k
Dhina k

Reputation: 1489

If you want to back from a child activity to Fragment put the following code in your Manifest File

  android:launchMode="singleTop" 

Upvotes: 7

Raghunandan
Raghunandan

Reputation: 133560

Activity EvtTransactionActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data>  element in your manifest?)

Change

    <activity
        android:name="com.android.nfcinfo2.EvtTransactionActivity"
        android:label="@string/title_activity_evt_transaction"
        android:parentActivityName="com.android.nfcinfo2.FragmentCeSupport" >// is this the activity you want to go to. i guess this is a fragment
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.android.nfcinfo2.FragmentCeSupport" />
    </activity>

to

    <activity
        android:name="com.android.nfcinfo2.EvtTransactionActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.android.nfcinfo2.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.android.nfcinfo2.MainActivity" />
    </activity>

Upvotes: 1

Related Questions