如何导航到父级活动 [英] How to navigate to a parent activity

查看:146
本文介绍了如何导航到父级活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我正在处理某件事情时,我需要在我的应用程序中配置动作栏
i从 http://developer.android .com
,我发现我正在寻找

Well when im working on something and i need to configure the action bar in my app i started from the http://developer.android.com and i found what i am looking for

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
    NavUtils.navigateUpFromSameTask(this);
    return true;
}
return super.onOptionsItemSelected(item);}

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

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

我做了所有这个
,但是当我的程序我按操作栏中的向上按钮程序崩溃
,这里是logcat

i did all of this but when on my program i press the up button in the action bar the program crashes and here is the logcat

09-04 12:54:02.087: E/AndroidRuntime(11033): FATAL EXCEPTION: main
09-04 12:54:02.087: E/AndroidRuntime(11033): java.lang.IllegalArgumentException: Activity LegendActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data>  element in your manifest?)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:177)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at com.yay.android.projects.stories.LegendActivity.onOptionsItemSelected(LegendActivity.java:44)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.app.Activity.onMenuItemSelected(Activity.java:2611)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:206)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.view.View.performClick(View.java:4261)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.view.View$PerformClick.run(View.java:17356)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.os.Handler.handleCallback(Handler.java:615)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.os.Looper.loop(Looper.java:137)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.app.ActivityThread.main(ActivityThread.java:4921)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at java.lang.reflect.Method.invoke(Method.java:511)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at dalvik.system.NativeStart.main(Native Method)

就像他们说
一样,我已经改变了相应名字的活动名称,我有
是什么这个问题在这里?

everything is just like they said ofcourse i have changed the activity names corresponding to names i have what's the problem here?

推荐答案

你必须在Maniifest文件中设置父活动

You have to set the parent activity inside the Maniifest file

<activity
            android:name="com.example.MainActivity"
            android:label="@string/title_activity_main"
            android:parentActivityName="com.example.MainUIActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.SampleActivity" />
        </activity>

或使用Support-V4如下

Or use the Support-V4 as follows

NavUtils.navigateUpTo(sourceActivity, upIntent);

或尝试在这篇文章

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        Intent upIntent = new Intent(this, YourListActivity.class);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is not part of the application's task, so
            // create a new task
            // with a synthesized back stack.
            TaskStackBuilder
                    .from(this)
                    .addNextIntent(new Intent(this, HomeActivity.class))
                    .addNextIntent(upIntent).startActivities();
            finish();
        } else {
            // This activity is part of the application's task, so simply
            // navigate up to the hierarchical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
    }
}

这是使用android更少的最佳方法比API级别11

This is the best way to do so with android less than API level 11

这篇关于如何导航到父级活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆