我一直在研究Android SDK平台,有点不清楚如何保存应用程序的状态.因此,考虑到'Hello,Android'示例的这种小型重新设计:
package com.android.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
private TextView mTextView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
if (savedInstanceState == null) {
mTextView.setText("Welcome to HelloAndroid!");
} else {
mTextView.setText("Welcome back.");
}
setContentView(mTextView);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这对于最简单的情况就足够了,但无论我如何远离应用程序,它总是以第一条消息响应.
我确信解决方案就像覆盖onPause或类似的那样简单,但我已经在文档中捅了大约30分钟左右,并且没有找到任何明显的东西.
我的应用程序中有活动A,B和C,它们按顺序流动.如果我在C中实现Up按钮,我希望它返回到B. 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)
但是,B期望在其onCreate方法中从A传递额外内容.B和C之间的关系是C允许用户设置一些影响B显示方式的设置.我目前正在处理在onPause()中保存C中的更改.当用户按下Up而不是调用navigateUpFromSameTask(this)时,我应该完成()C吗?