0

私はクリケットのアプリケーションを開発しています。戻るボタンを使用すると問題が発生します。私の目的は、ユーザーが戻るボタンをクリックしたときにデータを保存することです。ユーザーがクリックback buttonするか、別のアクティビティに移動すると、現在のアクティビティ データが表示されません。共有設定を使用してデータを保存しました。しかし、それでも私は問題に直面しています..誰かが何か考えを与えることができます. そのため、アプリケーションに実装します。

OnResume()ユーザーが戻るボタンを押したときに、保存されたデータを取得するために使用しています。

4

3 に答える 3

0

startActivityForResult最初のアクティビティから 2 番目のアクティビティを起動するときに使用します。2回目の活動で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ます。

于 2013-04-06T06:05:12.517 に答える
0

最初のアクティビティで、次のように 2 番目のアクティビティを呼び出します。

startActivityForResult(Intent_To_Second_Activity, 104);

2 番目のアクティビティでは、これを使用して押し戻します。

    @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

これは 2 番目のアクティビティのレイアウトです

<?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);
            }
        });
    }
}

2 番目のアクティビティの 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();
    }
}

メインフェスト

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

私は日食でコードをコーディングしましたが、それは機能しています。

于 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 に答える