ฉันต้องโหลดกิจกรรมจากกิจกรรมอื่นในแอปพลิเคชัน Android ใหม่ กิจกรรมที่ 2 นั้นเป็นหน้าต่างโต้ตอบ และกิจกรรมฐานของหน้าต่างโต้ตอบควรรีเฟรชเนื้อหา (โดยคลิกที่ปุ่มกิจกรรมโต้ตอบ)
รีเฟรชกิจกรรมจากกิจกรรมอื่น Android
คำตอบ (3)
โหลดกิจกรรมของคุณอีกครั้ง
context.startActivity(context.getIntent());
context.finish();
พิจารณาว่า A เป็นกิจกรรมที่คุณต้องการโหลดซ้ำ และ B เป็นกิจกรรมอื่น
ในกรณีนี้ เพียงโทร finish() เมื่อคุณเคลื่อนที่จาก A ไป B เมื่อคุณโทร A จาก B กิจกรรม A จะโหลดอีกครั้ง
หน้าต่างโต้ตอบและกิจกรรมเป็นสิ่งที่คล้องจองกันโดยสิ้นเชิง
คุณสามารถรีเฟรชกิจกรรมแรกจากกล่องโต้ตอบได้ คุณสามารถทำได้โดยใช้อินเทอร์เฟซ
นี่คือวิธีแก้ปัญหาง่ายๆ ในกิจกรรมแรกของคุณใช้ชื่ออินเทอร์เฟซ IRefreshInteface
คำจำกัดความของมันเหมือนกับการร้อง:
public interface IRefreshInteface(){
public void doRefreshValue(String commandValue);
}
ตอนนี้ หากคุณใช้ IRefreshInteface ในกิจกรรมของคุณ คุณจะได้รับวิธี doRefreshValue(String commandValue)
และเขียนโค้ดรีเฟรชที่นี่
@Override
void doRefreshValue(String commandValue){
// Write refresh code here
}
ตอนนี้ในกล่องโต้ตอบของคุณ คุณจะมีบริบทของกิจกรรมของคุณ การใช้วัตถุบริบทนั้นทำให้คุณสามารถเรียกเมธอด doRefreshValue(String commandValue)
นี้ได้อย่างง่ายดาย นี่คือโค้ดตัวอย่าง:
public AlertDialog displayMessage(Context context, String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
LayoutInflater inflater = LayoutInflater.from(context);
final View v = inflater.inflate(R.layout.custom_view, null);
builder.setView(v);
shipText = (EditText)v.findViewById(R.id.shipNameEditText);
scientistNameText = (EditText)v.findViewById(R.id.scientistEditText);
scientistEmailText = (EditText)v.findViewById(R.id.emailEditText);
volumeText = (EditText)v.findViewById(R.id.volumeEditText);
colourText = (EditText)v.findViewById(R.id.colourEditText);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((IRefreshInteface) context).doRefreshValue("YOUR_COMMAND");
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog));
return dialog;
}
ที่นี่ ส่ง Context of Activity ไปที่เมธอด displayMessage()
และเรียกเมธอดกิจกรรม doRefreshValue
โดยใช้วิธีนี้:
((IRefreshInteface) context).doRefreshValue("YOUR_COMMAND");
สำหรับข้อมูลเพิ่มเติม โปรดไปที่ที่นี่ และ ที่นี่
หวังว่านี่จะช่วยแก้ปัญหาของคุณได้ ขออภัยสำหรับภาษาอังกฤษที่ไม่ดี :)
startActivityForResult
ในกิจกรรมแรกหรือไม่? - person OneCricketeer   schedule 03.05.2017onActivityResult
เพื่อทำการรีเฟรช - person OneCricketeer   schedule 03.05.2017