我是Android开发的新手,正在关注" Adding Action Bar Tutorial"
我试图获取操作栏上的主页(后面的胡萝卜)按钮,以使我的应用程序返回主要活动。
在教程中提到,如果在xml中定义了父项,则可以在不处理向上按钮事件的情况下完成此操作。
下面是我的xml文件
<activity
android:name="com.example.firstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
但按下胡萝卜按钮不起作用。我能做错什么?
编辑: 添加以下活动代码
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* setContentView(R.layout.activity_display_message); if
* (savedInstanceState == null) {
* getSupportFragmentManager().beginTransaction() .add(R.id.container,
* new PlaceholderFragment()).commit(); }
*/
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
/*
* switch (item.getItemId()) { case android.R.id.home: Intent intent =
* new Intent(getApplicationContext(),
* com.example.firstapp.MainActivity.class);
* intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
* startActivity(intent); return true; default: return
* super.onOptionsItemSelected(item); }
*/
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
MainActivity低于
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
EditText temp1 = (EditText) findViewById(R.id.edit_message);
temp1.setText("Search");
return true;
case R.id.action_settings:
// openSettings();
EditText temp2 = (EditText) findViewById(R.id.edit_message);
temp2.setText("Settings");
return true;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
由于
答案 0 :(得分:1)
@Newton如果您正在使用parentActivityName属性,那么调用ActionBar.setDisplayHomeAsUpEnabled(true)实际上是多余的。也就是说,当您从DisplayMessageActivity中删除该行时,是&# 34;向上&#34;可供性仍然存在? - adneal
答案 1 :(得分:0)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 2 :(得分:0)
在您的活动中使用
getActionBar().setDisplayHomeAsUpEnabled(true);