返回 android 中的上一个 activity
Back to previous activity in android
我有 Activity A、B 和 C。在 Activity C 中,它有图像和标题。单击 ok
button
时,图像和标题将 return 变为 B。在activityb中,它会return都变成activityA.
Activity 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();
}
});
Activity 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();
}
});
Activity一个
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;
}
当我点击 Activity B 中的 button
b 时,我得到这个
11-05 17:26:47.114 6031-6031/com.example.project.project
E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!
假设您要做的只是将从 C->B 返回的数据传递给 A,为什么不在您的 onClick 中再次获取该字符串并将其放入您的意图中?
您可以将文本存储在 String 成员中,或者,如代码现在所示,甚至可以从 TextView 中取回它:
Intent returnIntent = new Intent();
returnIntent.putExtra("text", txt1.getText().toString);
setResult(Activity.RESULT_OK, returnIntent);
图像作为静态存储在全局对象中,老实说这很糟糕,但您当然也可以从 onClickListener 访问它。但是,您应该认真考虑以其他方式返回位图数据,最直接的可能是利用位图实现 Parcelable 接口,并且 Intents 可以将 Parcelables 作为额外内容保存,因此理论上您可以只做 putExtra("img", bitmap)
并在接收端做intent.getParcelableExtra("img")
。
但是,如果位图很大,这可能会失败,最好将位图存储到文件等资源中,然后传递文件的位置。您也可以通过在 Global class 中对静态成员所做的事情来获得某种方法,但是 1) 在将对象传回后必须小心删除该对象,否则静态引用会浪费内存,并且 2)确保您没有错误地使用此参考,例如同时来自多个地方。一个更强大的解决方案是确保为每个位图创建一个唯一的 ID 并将其存储在缓存中(例如以位图 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);
在你的 Activity B
您可以对所有 Activity 使用 Global.img 而不是使用 intent
已添加:
v.setImageBitmap(Global.img);
-> nullPointerExeption 因为 v
没有初始化
已添加:
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPreferredConfig = Config.RGB_565;
bitmapOptions.inDither = true;
"Out of memory" 错误。
我有 Activity A、B 和 C。在 Activity C 中,它有图像和标题。单击 ok
button
时,图像和标题将 return 变为 B。在activityb中,它会return都变成activityA.
Activity 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();
}
});
Activity 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();
}
});
Activity一个
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;
}
当我点击 Activity B 中的 button
b 时,我得到这个
11-05 17:26:47.114 6031-6031/com.example.project.project E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!
假设您要做的只是将从 C->B 返回的数据传递给 A,为什么不在您的 onClick 中再次获取该字符串并将其放入您的意图中? 您可以将文本存储在 String 成员中,或者,如代码现在所示,甚至可以从 TextView 中取回它:
Intent returnIntent = new Intent();
returnIntent.putExtra("text", txt1.getText().toString);
setResult(Activity.RESULT_OK, returnIntent);
图像作为静态存储在全局对象中,老实说这很糟糕,但您当然也可以从 onClickListener 访问它。但是,您应该认真考虑以其他方式返回位图数据,最直接的可能是利用位图实现 Parcelable 接口,并且 Intents 可以将 Parcelables 作为额外内容保存,因此理论上您可以只做 putExtra("img", bitmap)
并在接收端做intent.getParcelableExtra("img")
。
但是,如果位图很大,这可能会失败,最好将位图存储到文件等资源中,然后传递文件的位置。您也可以通过在 Global class 中对静态成员所做的事情来获得某种方法,但是 1) 在将对象传回后必须小心删除该对象,否则静态引用会浪费内存,并且 2)确保您没有错误地使用此参考,例如同时来自多个地方。一个更强大的解决方案是确保为每个位图创建一个唯一的 ID 并将其存储在缓存中(例如以位图 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);
在你的 Activity B
您可以对所有 Activity 使用 Global.img 而不是使用 intent
已添加:
v.setImageBitmap(Global.img);
-> nullPointerExeption 因为 v
没有初始化
已添加:
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPreferredConfig = Config.RGB_565;
bitmapOptions.inDither = true;
"Out of memory" 错误。