从片段开始的活动返回

时间:2014-01-16 12:56:43

标签: java android eclipse android-fragments

我有一个使用Tabs的应用程序。我使用Fragments来实现TabListeners。其中一个片段开始2个活动(取决于一些按钮)。

我使用Eclipse接口创建这些活动,这意味着Eclipse负责所有工作(创建布局,更新清单等)。

如果我使用手机上的返回按钮从活动返回,我没有任何问题。 但是,如果我使用“<”活动布局左上角的符号,我有一个错误:

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)

但看起来我的清单是好的(FragmentCeSupport是父级,EvtTransaction和SeRouting是子活动):

  <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>

以下是我如何从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;
    }
}

这是EvtTransactionActivity中的返回代码(由Eclipse生成):

@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);
}

2 个答案:

答案 0 :(得分:6)

如果要从子活动返回到Fragment,请将以下代码放入清单文件

  android:launchMode="singleTop" 

有关详情,请参阅here。 快乐编码:)

答案 1 :(得分:1)

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?)

更改

    <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>

    <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>