【发布时间】: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