所以这里的情况我有活动A和片段userprofile,当用户点击编辑时,片段被editprofile替换,所以当用户点击图像按钮时,执行下面的代码从库中选择图像。 关闭活动时的问题,它返回片段userprofile。 Activty A不是使用片段editprofile而是使用userprofile恢复的。
使片段容器膨胀的主要活动代码
public class Main extends AppCompatActivity {
//String institudeID;
String institudeID;
Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton profile = (ImageButton) findViewById(R.id.profile);
profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
bundle.putString("id", institudeID);
UserProfile newFragment = new UserProfile();
newFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.replace(R.id.fragement_container, newFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
});
ImageButton discussion = (ImageButton) findViewById(R.id.discussionthread);
discussion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
bundle.putString("id", institudeID);
Discussion_Main newFragment = new Discussion_Main();
newFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.replace(R.id.fragement_container, newFragment);
transaction.commit();
}
});
ImageButton news = (ImageButton) findViewById(R.id.news);
news.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
bundle.putString("id", institudeID);
News_Main newFragment = new News_Main();
newFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.replace(R.id.fragement_container, newFragment);
transaction.commit();
}
});
ImageButton b1 = (ImageButton) findViewById(R.id.b1);
bhhooo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
bundle.putString("id", institudeID);
BHHOOO_Main newFragment = new BHHOOO_Main();
newFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.replace(R.id.fragement_container, newFragment);
transaction.commit();
}
});
ImageButton chat = (ImageButton) findViewById(R.id.chat);
chat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
bundle.putString("id", institudeID);
Chat_Main newFragment = new Chat_Main();
newFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.replace(R.id.fragement_container, newFragment);
transaction.commit();
}
});
}
@Override
public void onStart(){
super.onStart();
institudeID = getIntent().getStringExtra("id");
//Log.i("LOG", institudeID);
if(getIntent().hasExtra("register") == true){
Bundle bundle = new Bundle();
bundle.putString("id",institudeID);
bundle.putBoolean("register", true);
Edit_Profile newFragment = new Edit_Profile();
newFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.add(R.id.fragement_container, newFragment);
transaction.commit();
}
else {
bundle = new Bundle();
bundle.putString("id", institudeID);
News_Main newFragment = new News_Main();
newFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.add(R.id.fragement_container, newFragment);
transaction.commit();
}
}
@Override
public void onDestroy(){
super.onDestroy();
}}
从这里当用户按下配置文件按钮时,片段容器用userprofile充气,然后用户可以单击editprfile,用户自己替换userprofile,用户可以选择图像
编辑个人资料的代码
public class Edit_Profile extends Fragment {
com.facebook.Profile userProfile;
EditText editName;
EditText editJob;
EditText editBio;
EditText editEmail;
EditText editPhone;
int SELECT_IMAGE = 1; // global
ImageView profilePicture;
String selectedImagePath;
Bitmap pro_Pic;
File imgFile;
FirebaseStorage storage;
StorageReference storageRef;
String userName;
String job = null;
String bio = null;
String email = null;
String phone = null;
String institudeID;
Bundle bundle;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
bundle = this.getArguments();
institudeID = bundle.getString("id");
return inflater.inflate(R.layout.activity_edit_profile, container, false);
}
@Override
public void onStart() {
super.onStart();
initialize();
ImageButton imageButton = (ImageButton) getActivity().findViewById(R.id.editback);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().popBackStack();
}
});
ImageButton imageButton1 = (ImageButton) getActivity().findViewById(R.id.editaccept);
imageButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(institudeID).child("User")
.child(userProfile.getId().toString());
User user = new User(editName.getText().toString(), editJob.getText().toString(), editBio.getText().toString()
, "Profile/P" + userProfile.getId().toString() + ".jpg", editEmail.getText().toString()
, editPhone.getText().toString());
ref.setValue(user);
uploadImage();
}
});
ImageView pic = (ImageView) getActivity().findViewById(R.id.profilePic);
pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
}
});
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
}
public void initialize(){
userProfile = com.facebook.Profile.getCurrentProfile();
//selectedImagePath = "https://graph.facebook.com/" + userProfile.getId() + "/picture?type=large";
profilePicture = (ImageView) getActivity().findViewById(R.id.profilePic);
editName = (EditText) getActivity().findViewById(R.id.editName);
editJob = (EditText) getActivity().findViewById(R.id.editJob);
editBio = (EditText) getActivity().findViewById(R.id.editBio);
editEmail = (EditText) getActivity().findViewById(R.id.editEmail);
editPhone = (EditText) getActivity().findViewById(R.id.editPhone);
storage = FirebaseStorage.getInstance();
storageRef = storage.getReferenceFromUrl("gs://thread-4cc4e.appspot.com");
if(bundle.getBoolean("register", false)){
Picasso.with(getActivity().getApplicationContext())
.load("https://graph.facebook.com/" + userProfile.getId() + "/picture?type=large")
.transform(new CropCircleTransformation())
.into(profilePicture);
}else{
retrieveProfile();
}
}
public void retrieveProfile(){
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(institudeID).child("User")
.child(Profile.getCurrentProfile().getId().toString());
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
userName = user.getName();
job = user.getJob();
bio = user.getBio();
email = user.getEmail();
phone = user.getPhoneNo();
editName.setText(userName);
editJob.setText(job);
editBio.setText(bio);
editEmail.setText(email);
editPhone.setText(phone);
downloadImage();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
ref.addListenerForSingleValueEvent(valueEventListener);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_IMAGE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
Picasso.with(getActivity().getApplicationContext())
.load(selectedImageUri)
.transform(new CropCircleTransformation())
.into(profilePicture);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public void uploadImage(){
StorageReference profileImagesRef = storageRef.child("Profile/P" + userProfile.getId().toString() + ".jpg");
profilePicture.setDrawingCacheEnabled(true);
profilePicture.buildDrawingCache();
Bitmap bitmap = profilePicture.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = profileImagesRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});
}
void downloadImage(){
StorageReference gsReference = storage.getReferenceFromUrl("gs://thread-4cc4e.appspot.com/Profile/P"
+ userProfile.getId().toString() + ".jpg");
gsReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.with(getActivity().getApplicationContext())
.load(uri)
.transform(new CropCircleTransformation())
.resize(400, 400)
.into(profilePicture);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
});
}}
答案 0 :(得分:1)
从点击侦听器中删除startActivity(intent);
。 startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
用于开始活动并在onActivityResult()
中获取结果。您无需再次致电startActivity()
。
discPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
// **startActivity(intent);**
}
});
我已经检查了您的代码,您正在使用OnStart()
方法进行片段转换。因此,当您的控件从图库返回时,再次调用onStart()
。
请在onCreate()
中写下您的Fragmnet替换逻辑,或者为onStart()
逻辑维护布尔值。
当您的控制权返回应用程序时,会调用这些活动生命周期方法
onRestart() --> onStart() --> onResume()
OnCreate() call only once, so do your code here for fragment replace
答案 1 :(得分:0)
我认为问题出在这里
startActivityForResult(Intent.createChooser(intent,&#34; Select Picture&#34;),SELECT_IMAGE); startActivity(意向);
您正在同时调用startActivityForResult和startActivity。使用ONly startActivityForResult。
并在&#34; onActivityResult&#34;上进行更改并在代码中包含super.onActivityResult(requestCode,resultCode,data),然后尝试它是否适合您