如何在移动到另一个 activity 并返回时保存 activity 的状态?

how save a state of activity when move to another activity and back?

请帮助我如何保存 Activity 的状态?

我解释一下我的问题:

我的计划中有很多活动

我通过以下代码将数据从 Activity 发送到 "Dr_see_Patient_Pro" Activity:

  Intent i=new Intent(this,Dr_see_Patient_Pro.class);
    i.putExtra("idPFf2",id);
    startActivity(i);

所以我以这种方式在 "Dr_see_Patient_Pro" Activity 中检索此数据:

  idPatient= getIntent().getExtras().getString("idPFf2");
    int id=Integer.parseInt(idPatient);

我没有任何问题

之后

我想发送 "idPatient" In "Dr_see_Patient_Pro" Activity To Another Activity which is (write_history) Activity 通过以下代码:

 Intent intent=new Intent(this,write_History.class);
    intent.putExtra("idPFf3",idPatient);
    startActivity(intent);

而且我在 "write_history" Activity

中检索 "idPatient" 没有问题

但是

当我想从 "write_history" Activity 返回到 "Dr_see_Patient_Pro" Activity 我有一个问题并且这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.Dr_see_Patient_Pro}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    at android.app.ActivityThread.access0(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
    at com.example.myapplication.Dr_see_Patient_Pro.onCreate(Dr_see_Patient_Pro.java:37)
    at android.app.Activity.performCreate(Activity.java:5008)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

问题出在第 37 行及其第 37 行的代码:

        System.out.println(idPatient);

它是我的活动的代码: 1/ Dr_see_Patient_Pro

public class Dr_see_Patient_Pro extends AppCompatActivity {

TextView tx, tx1, tx2, tx3, tx4, tx5, tx6;
public static String getInList;
MyDB db;

public  String idPatient;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dr_see__patient__pro);

    db = new MyDB(this);

    tx=(TextView)findViewById(R.id.dr_nameP);
    tx1=(TextView)findViewById(R.id.dr_ageP);
    tx2=(TextView)findViewById(R.id.dr_genderP);
    tx3=(TextView)findViewById(R.id.dr_cityP);
    tx4=(TextView)findViewById(R.id.dr_phoneP);




     idPatient= getIntent().getExtras().getString("idPFf2");
    System.out.println(idPatient);

    int id=Integer.parseInt(idPatient);


    ///phone

    Cursor dataname = db.getPaientInfo(id);

    while (dataname.moveToNext()) {
        String name1 = dataname.getString(0);
        String name2 = dataname.getString(1);
        tx.setText(name1+" "+name2);
    }



    Cursor dataPhone = db.getPaientInfo(id);
    while (dataPhone.moveToNext()) {
        String name2 = dataPhone.getString(2);
        tx4.setText(name2);
    }


    Cursor dataAge = db.getPaientInfo(id);
    while (dataAge.moveToNext()) {
        String name2 = dataAge.getString(3);
        tx1.setText(name2);
    }


    Cursor dataGender = db.getPaientInfo(id);
    while (dataGender.moveToNext()) {
        String name2 = dataGender.getString(4);
        tx2.setText(name2);
    }


    Cursor dataCity = db.getPaientInfo(id);
    while (dataCity.moveToNext()) {
        String name2 = dataCity.getString(5);
        tx3.setText(name2);
    }







}


public void writeHistory(View v){
    Intent intent=new Intent(this,write_History.class);
    intent.putExtra("idPFf3",idPatient);
    startActivity(intent);
}



public void SeeHistory(View view){
    Intent intent=new Intent(this,see_history.class);
    startActivity(intent);
}

2/write_history

public class write_History extends AppCompatActivity {

    MyDB myDB;
    String raport,charasar;
    AlertDialog.Builder builder;

    TextInputLayout edt;
    TextInputEditText edt1;

    EditText edt2;

    public String drID,DrIdS;
    public  int idDr;
public  int id;
public  String idPatient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write__history);

        myDB = new MyDB(this);

        builder = new AlertDialog.Builder(this);

        edt=findViewById(R.id.edt1);
        edt1=findViewById(R.id.edtText1);


        edt2=findViewById(R.id.edt2);


         idPatient= getIntent().getExtras().getString("idPFf3");
        System.out.println(idPatient);
         id=Integer.parseInt(idPatient);



        drID= loginPage.Dr();
        Cursor data = myDB.getIDdr(drID);
        while (data.moveToNext()) {
            DrIdS = data.getString(0);
            idDr=Integer.parseInt(DrIdS);
        }


        System.out.println("dr id " + idDr+"   p "+id);




    }



    private boolean validat() {
        String emailInput = edt1.getText().toString();

        if (emailInput.isEmpty()) {
            edt.setError("Field can't be empty");
            return false;
        }

        else {
            edt.setError(null);
            return true;
        }
    }



    public void saveHistory(View view){
        if( !validat()) {
            return;
        }
else {
            DialogMassage();

        }

    }





    public void DialogMassage() {
        myDB = new MyDB(this);

        raport=edt1.getText().toString();
        charasar=edt2.getText().toString();

        setTitle("ئاگاداربە!");
        builder.setMessage("ئایا دڵنیای لە پاشەکەوت کردنی زانیاریەکان؟")
                .setCancelable(false) .setPositiveButton("بەڵێ", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        Boolean result=myDB.SaveHistory(raport,charasar,idDr,id);
                        System.out.println(idDr+" "+id);
                        changeA();


                    }
                })
.setNegativeButton("نەخێر", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
        }
        });

        AlertDialog alert = builder.create();
        alert.show();

    }


    public void changeA(){

        Intent i=new Intent(this,Dr_see_Patient_Pro.class);
        i.putExtra("idPFf2",id);
        startActivity(i);

    }


}

请帮我找到解决办法!

您似乎正在从您的 activity "Dr_see_Patient_Pro" 中传递 "int" 并试图在您的 "write_history" [=18= 中检索 "String" 类型] 所以你得到空指针异常。所以我建议您在 "write_history" activity.

中尝试以下代码
id=getIntent().getIntExtra("idPFf3");