[英]Back to previous activity in android
我有活動A,B和C。 在活動C中,它具有圖像和標題。 單擊ok
button
時,圖像和標題將返回B。 在活動b中,它將同時返回活動A。
活動C
ok.setOnClickListener(new View.OnClickListener() // ok button to return image and caption to B
{
public void onClick(View arg0)
{
Intent returnIntent=new Intent();
text=t.getText().toString();
b.setDrawingCacheEnabled(true);
b.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
b.layout(0, 0, b.getMeasuredWidth(), b.getMeasuredHeight());
b.buildDrawingCache(true);
returnIntent.putExtra("text", text);
if (b.getDrawingCache() != null) {
Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
if (bitmap == null) {
Log.e("TAG", "getDrawingCache() == null");
}
Global.img = bitmap;
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
活動B
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
if(requestCode==PROJECT_REQUEST_CODE) {
if(data!=null&&data.hasExtra("text")) {
c = data.getStringExtra("text");
txt1.setText(c); // caption can show in txt1
viewImage.setImageBitmap(Global.img); // image from C can show in viewImage
}
}
else if (requestCode==CAMERA_REQUEST_CODE)
{
}
}
b.setOnClickListener(new View.OnClickListener() { // button to return image and caption to A
public void onClick(View arg0) {
Intent returnIntent = new Intent();
returnIntent.putExtra("c",c); // return caption
returnIntent.putExtra("globalImg",Global.img);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
活動A
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap b = Global.img
if (requestCode==1) {
caption = data.getStringExtra("c"); // caption from A
v.setImageBitmap(Global.img) // image from C
}else if {.....}
}
Global.java
public class Global {
static Bitmap img;
}
當我在活動B中單擊button
b時,我得到了
11-05 17:26:47.114 6031-6031 / com.example.project.project E / JavaBinder:!!! 綁定交易失敗!!!
如果您只是想將C-> B返回的數據傳遞給A,那么為什么不將字符串再次放入onClick中並放入您的意圖中呢? 您可以將文本存儲在String成員中,或者按照代碼現在的樣子,甚至可以從TextView中獲取它:
Intent returnIntent = new Intent();
returnIntent.putExtra("text", txt1.getText().toString);
setResult(Activity.RESULT_OK, returnIntent);
圖像是作為靜態對象存儲在全局對象中的,老實說這很可怕,但是您當然也可以從onClickListener訪問它。 但是,您應該認真考慮以其他方式返回位圖數據,最直接的可能就是利用位圖實現Parcelable接口,並且Intent可以將putExtra("img", bitmap)
保留為額外內容,因此從理論上講,您可以只做putExtra("img", bitmap)
在接收端執行intent.getParcelableExtra("img")
。
但是,如果位圖很大,則可能會失敗,因此最好將位圖存儲到資源(例如文件)中,然后傳遞文件的位置。 您也可能會獲得與Global類中的靜態成員相同的方式,但是1)您必須小心地將對象傳回之后刪除該對象,否則靜態引用將浪費內存; 2)確保您不不要錯誤地使用此引用,例如模擬地從多個地方使用。 一個更可靠的解決方案是確保為每個位圖創建一個唯一的ID,並將其存儲在緩存中(例如,以Bitmaps hashCode()為鍵的HashMap)並通過ID進行標識。
public class BitmapStore {
private static final HashMap<Integer, Bitmap> BITMAPS= new HashMap<>();
public static Bitmap getBitmap(int id) {
return BITMAPS.remove(id);
}
public static int putBitmap(Bitmap b) {
int id = b.hashCode();
BITMAPS.put(id, b);
return id;
}
}
使用此方法,可以將位圖的ID放入意圖中:
intent.putExtra("imageId", BitmapStore.putBitmap(bitmap));
setResult(RESULT_OK, intent);
並在onActivityResult
:
Bitmap b = BitmapStore.getBitmap(intent.getIntExtra("imageId", 0));
該位圖存儲不是持久性的,但在兩個活動之間的過渡中用作中間存儲應該是相當安全的。 也有可能推廣到任何實現有效hashCode / equals合同的對象。
嘗試對此行發表評論:
//returnIntent.putExtra("globalImg",Global.img);
在您的活動B中
您可以對所有活動使用Global.img,而不能使用意圖
添加:
v.setImageBitmap(Global.img);
-> nullPointerExeption因為v
沒有初始化
添加:
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPreferredConfig = Config.RGB_565;
bitmapOptions.inDither = true;
對於“內存不足”錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.