0

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

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

4

3 回答 3

0

Use startActivityForResult when launching second activity from first activity. In second activity 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);

}  

In first activity

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

Then 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
     } 

}  

The onActivityResult is called before onResume.

于 2013-04-06T06:05:12.517 回答
0

In your first activity call your second activity like this:

startActivityForResult(Intent_To_Second_Activity, 104);

In your second activity when you press back use this:

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

Complete working example

Main.xml

this is the layout for your first activity

<?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

this is the layout for your second activity

<?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 code for your first activity

the class name is 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 code for your second activity

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>

I have codedthe code in my eclipse and it is working.

于 2013-04-06T06:14:23.187 回答
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">

享受..

于 2013-04-06T06:27:30.683 回答