Android保存当前活动

时间:2013-04-06 06:00:02

标签: android

我正在开发板球应用程序。当我使用后退按钮时,我遇到问题。我的目标是在用户单击后退按钮时保存数据。当用户单击back button或转到另一个活动时,当前活动数据不会显示。我使用Shared首选项来保存数据。但我仍然面临着这个问题..任何人都可以有一些想法。所以我将在我的申请中实施。

当用户按下后退按钮时,我正在使用OnResume()来检索已保存的数据。

3 个答案:

答案 0 :(得分:0)

从第一个活动启动第二个活动时使用startActivityForResult。第二项活动onBackPressed()

@Override
public void onBackPressed()
{
    super.onBackPressed();

    Intent i = new Intent();
    i.putExtra("name", value);
    // put as many extra as you want to save
    setResult(RESULT_OK, i);

}  

在第一项活动中

startActivityForResult(new Intent(this, SecondActivity.class), 1);  

然后onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == 1 && resultCode == RESULT_OK)
     {
         // Using data to get the extra set in intent i in second activity
         // for example data.getStringExtra("name", ""); and so on
     } 

}  

onActivityResult之前调用onResume

答案 1 :(得分:0)

在您的第一个活动中,请拨打您的第二个活动:

startActivityForResult(Intent_To_Second_Activity, 104);

在您按下的第二个活动中使用此功能:

    @Override
public void onBackPressed()
{
    super.onBackPressed();
    finish();
    // Save your data here
}

完整的工作示例

main.xml中

这是您第一次活动的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="click"/>

</LinearLayout>

second.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

第一个活动的Java代码

班级名称为StackActivity

package com.stack.question;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class StackActivity extends Activity {

    EditText et;
    Button bt;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et = (EditText) findViewById(R.id.editText);
        bt = (Button) findViewById(R.id.button);
        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(StackActivity.this, Second.class);
                startActivityForResult(intent, 104);
            }
        });
    }
}

第二个活动的Java代码

package com.stack.question;

import android.app.Activity;
import android.os.Bundle;

public class Second extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();
    }
}

Mainfest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stack.question"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".StackActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Second"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

我已经在我的日食中编码了代码并且它正在运行。

答案 2 :(得分:0)

我以这种方式解决了这个问题。

活动

@Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("In Destroy()");
        sharedPref = getSharedPreferences("test",MODE_PRIVATE);
        prefEditor = sharedPref.edit(); 
        //save your data here.      
         prefEditor.commit();
    }
清单中的

<activity android:name=".MainActivity"  android:launchMode="singleTask">

享受..