【问题标题】:How to go to different page when I press back button on action bar?当我按下操作栏上的返回按钮时如何转到不同的页面?
【发布时间】:2016-05-27 16:10:11
【问题描述】:

我正在尝试转到 ProfileFragment 页面,但是当我尝试按操作栏上的后退按钮时,它会转到 LoginActivity,这是另一个不相关的活动。我该怎么做?

代码如下: 我的 SettingsActivity.java

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;


public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        Typeface ubuntu_MI = Typeface.createFromAsset(getAssets(), "Ubuntu-MI.ttf");

        // Get the ActionBar
        android.support.v7.app.ActionBar ab = getSupportActionBar();

        // Create a TextView programmatically.
        TextView tv = new TextView(getApplicationContext());

        // Create a LayoutParams for TextView
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, // Width of TextView
                RelativeLayout.LayoutParams.WRAP_CONTENT); // Height of TextView

        // Apply the layout parameters to TextView widget
        tv.setLayoutParams(lp);

        // Set text to display in TextView
        // This will set the ActionBar title text
        tv.setText("Ayarlar");

        // Set the text color of TextView
        // This will change the ActionBar title text color
        tv.setTextColor(Color.parseColor("#FFF5EE"));

        // Center align the ActionBar title
        tv.setGravity(Gravity.CENTER_HORIZONTAL);

        // Set the serif font for TextView text
        // This will change ActionBar title text font
        tv.setTypeface(ubuntu_MI);

        // Underline the ActionBar title text
        // tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);

        // Set the ActionBar title font size
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);

        // Display a shadow around ActionBar title text
        //tv.setShadowLayer(
        //      1.f, // radius
        //    2.0f, // dx
        //  2.0f, // dy
        //  Color.parseColor("#FF8C00") // shadow color
        //);

        // Set the ActionBar display option
        ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        // Finally, set the newly created TextView as ActionBar custom view
        ab.setCustomView(tv);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        //Action Bar'daki back butonuna basınca ne yapacağını söylüyoruz.
        if (item.getItemId() == android.R.id.home) {
            Intent intent = new Intent(getApplicationContext(), ProfileFragment.class);
            NavUtils.navigateUpTo(this, intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    }

【问题讨论】:

  • 您无法创建通向 Fragment 的 Intent,您只能导航到活动。
  • 那么,我该如何解决这个问题?
  • 为包含 ProfileFragment 的 Activity 创建一个 Intent。
  • 您是否在活动中托管了任何片段?
  • 我有 CombineFragment Activity,它结合了 2 个不同的活动。其中一个是 StartingTestFragment,另一个是 ProfileFragment。我将在哪个页面创建 Intent 以及如何创建?因为后退按钮位于另一个名为 SettingsActivity 的活动中。我正在尝试将页面从 SettingsActivity 传递给 ProfileFragment。

标签: android android-fragments android-actionbar back-button


【解决方案1】:

我相信您说的是向上导航。在这种情况下,您可以为当前活动声明一个父活动,它会始终将您带到那里。

来自 Android 文档:

https://developer.android.com/training/implementing-navigation/ancestral.html

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

【讨论】:

  • 我要在 AndroidManifest.xml 中添加这些代码对吗?
  • 是的。有关详细信息,请参阅链接。
  • 我查看了链接,我看到了这一行: 我正在开发 4.0.0 及更高版本
  • 只需添加它以实现向后兼容性,否则您可以跳过该部分。
【解决方案2】:

您不能创建指向FragmentIntent。从SettingsActivity,您应该创建一个Intent 以转到托管ProfileFragmentCombineFragmentActivity

而不是写

Intent intent = new Intent(getApplicationContext(), ProfileFragment.class);

你应该写

Intent intent = new Intent(this, CombineFragmentActivity.class);

在您的 onOptionsItemSelected 函数中。

【讨论】:

猜你喜欢
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-08
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
相关资源
最近更新 更多