从片段开始的活动返回

Ale*_*lex 2 java eclipse android 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)
Run Code Online (Sandbox Code Playgroud)

但看起来我的清单是正常的(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>
Run Code Online (Sandbox Code Playgroud)

以下是我从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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是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);
}
Run Code Online (Sandbox Code Playgroud)

Dhi*_*a k 6

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

  android:launchMode="singleTop" 
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息参见这里.快乐编码:)