我有一个活动A,该活动使用Recyclerview和Firebase显示图像。当我单击图像时,它将在新的活动B中打开。但是,当我从活动B返回至A时,活动A从头开始。我想恢复活动A并从点击的图片开始。
我添加了此代码,但是它不起作用
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Manifests.xml
android:launchMode="singleInstance"
活动A:
package com.cornicore.imagestatus;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.io.ByteArrayOutputStream;
public class AttitideActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attitide);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Status");
mRecyclerView = findViewById(R.id.attitude_recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Attitude");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(
Model.class,
R.layout.row,
ViewHolder.class,
mRef
) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getImage());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnclickListener(new ViewHolder.ClickListener() {
@Override
public void onItemClick(View view, int position) {
ImageView mImageView = view.findViewById(R.id.rImageView);
Drawable mDrawable = mImageView.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
Intent intent = new Intent(view.getContext(), PostDetailsActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100,stream);
byte[] bytes = stream.toByteArray();
intent.putExtra("image", bytes);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
@Override
public void onItemLongClick(View view, int position) {
}
});
return viewHolder;
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
}
活动B:
package com.cornicore.imagestatus;
import android.app.WallpaperManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.Manifest;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.concurrent.Executor;
public class PostDetailsActivity extends AppCompatActivity {
Bitmap bitmap;
ImageView mImageTv;
Button mSaveBtn, mShareBtn, mWallBtn;
private static final int WRITE_EXTERNAL_STORAGE_CODE = 1;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Status Image");
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(0xf5000000));
Window window = getWindow();
window.setStatusBarColor(getResources().getColor(R.color.statusBlack));
mImageTv = findViewById(R.id.full_imageView);
mSaveBtn = findViewById(R.id.saveBtn);
mShareBtn = findViewById(R.id.shareBtn);
mWallBtn = findViewById(R.id.wallBtn);
byte[] bytes = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mImageTv.setImageBitmap(bmp);
bitmap = ((BitmapDrawable)mImageTv.getDrawable()).getBitmap();
mImageTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
mSaveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M){
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){
String[] permission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permission, WRITE_EXTERNAL_STORAGE_CODE);
}
else{
saveImage();
}
}
else {
saveImage();
}
}
});
mShareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareImage();
}
});
mWallBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setImgWallpaper();
}
});
}
private void setImgWallpaper() {
WallpaperManager myWallManager = WallpaperManager.getInstance(getApplicationContext());
try{
myWallManager.setBitmap(bitmap);
Toast.makeText(this, "Wallpaper set ...", Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void shareImage() {
try{
File file = new File(getExternalCacheDir(), "sample.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share Via"));
}
catch (Exception e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void saveImage() {
String timeStamp = new SimpleDateFormat("yyyymmdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis());
File path = Environment.getExternalStorageDirectory();
File dir = new File(path+"/Image_Status/");
dir.mkdirs();
String imageName = timeStamp + ".PNG";
File file = new File(dir, imageName);
OutputStream out;
try{
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
Toast.makeText(this, imageName+" saved to"+ dir, Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case WRITE_EXTERNAL_STORAGE_CODE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
saveImage();
}
else {
Toast.makeText(this, "enable permission to save image", Toast.LENGTH_SHORT).show();
}
}
}
}
}
答案 0 :(得分:1)
删除此:
}
@Override
protected void onStart() {
super.onStart();
来自 A
答案 1 :(得分:1)
出现问题的原因是,当您返回到Activity时,将调用onStart()方法,但是在创建活动时将调用一次onCreate(),只需从onStart中删除逻辑 ()并将其添加到onCreate()