获得活动状态时遇到问题。我有2个活动:MainActivity和Activity2。在MainActivity中,我将editText
和button
名称设为GO。在Activity2中,我有一个button
名称BackMainActivity
。我想要的是:我把一个文本,例如:“abc”到EditText然后点击按钮GO。应用程序将导航到Activity2。之后,我点击BackMainActivity按钮,应用程序将导航到MainActivity,EditText中的数据将恢复为“abc”。
我已经使用过onSaveInstanceState和onRestoreInstanceState。应用程序在转到Activity2之前运行onSaveInstanceState。但是如果我回到MainActivity,onCreate(),savedInstanceState为null。你能告诉我原因吗?我想在bundle中存储mainActivity的数据。那我该怎么办?非常感谢你!
MainActivity
package com.example.demosavedataactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText t;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
String a = t.getText().toString();
outState.putString("text",a);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
t.setText(savedInstanceState.getString("text"));
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onDestroy");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onStop");
super.onStop();
}
}
活性2
package com.example.demosavedataactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity {
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Activity2.this, MainActivity.class);
startActivity(intent);
}
});
}
}
activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CombackA1" />
</LinearLayout>
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="18dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="50dp"
android:layout_marginTop="63dp"
android:text="Button" />
</RelativeLayout>
答案 0 :(得分:1)
做一些像:
1:从onSavedInstanceState
中的oncreate()
获取数据并将其放入edittext
。类似的东西:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
/*Just fetch data from savedInstanceState */
if(savedInstanceState != null){
t.setText(savedInstanceState.getString("text"));
}
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
}
在Activity2
中设置一个FLAG_ACTIVITY_REORDER_TO_FRONT
标记,其中包含您的意图。
Intent intent = new Intent(Activity2.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
为了更好的主意,请查看此conversation。
答案 1 :(得分:0)
在Activity2的onClick功能中,您不必返回上一个MainActivity但是您打开一个新的MainActivity。
通过调用finish()完成Activity2,然后您的第一个MainActivity将再次出现。它应该使用设备的硬件后退按钮。