切换到另一个片段后切换回片段时,更新后的ImageView会丢失

时间:2017-12-01 19:37:33

标签: java android

到目前为止,我设法让片段中的ImageView更新,以便在“活动”中选择它。但是现在当我从片段PeopleFragment切换到另一个片段TaskFragment然后又回来时,我选择的ImageView不再存在,而是加载了默认的ImageView片段首次初始化时。

PeopleFragment.java

public class PeopleFragment extends Fragment {
ImageButton profileImage;
private FirebaseAuth mAuth;
TextView fullName;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_people2, container, 
    false);

    profileImage = (ImageButton) view.findViewById(R.id.imageButton4);
    profileImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent settingsClick = new Intent(getActivity(), 
            ProfileSettingsActivity.class);
            startActivityForResult(settingsClick,0);
        }
    });

    fullName = (TextView) view.findViewById(R.id.userProfileFullName);
    mAuth = FirebaseAuth.getInstance();

    return view;
}


@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    updateUI(currentUser);
}

/**
 * Updates the view according to the authentication status.
 * @param user the current FirebaseUser
 */
private void updateUI(FirebaseUser user) {
    if (user != null) {
        fullName.setText(user.getDisplayName());
    }
}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_CANCELED) return;
    //Getting the Avatar Image we show to our users
    ImageView avatarImage = 
    (ImageView)getView().findViewById(R.id.imageButton4);
    //Figuring out the correct image
    String drawableName = "profile1";
    switch (data.getIntExtra("imageID",R.id.teamid00)) {
        case R.id.teamid00:
            drawableName = "profile1";
            break;
        case R.id.teamid01:
            drawableName = "profile2";
            break;
        case R.id.teamid02:
            drawableName = "profile3";
            break;
        case R.id.teamid03:
            drawableName = "profile4";
            break;
        case R.id.teamid04:
            drawableName = "profile5";
            break;
        case R.id.teamid05:
            drawableName = "profile6";
            break;
        default:
            drawableName = "profile1";
            break;
    }
    int resID = getResources().getIdentifier(drawableName, "drawable", 
    getActivity().getPackageName());
    avatarImage.setImageResource(resID);
}

}

ProfileActivity.java

public class ProfileSettingsActivity extends AppCompatActivity {
//ImageButton targetImage;
Button loadButton;

ImageView profileImage1;
ImageView profileImage2;
ImageView profileImage3;
ImageView profileImage4;
ImageView profileImage5;
ImageView profileImage6;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_settings);

    profileImage1 = (ImageView) findViewById(R.id.teamid00);
    profileImage2 = (ImageView) findViewById(R.id.teamid01);
    profileImage3 = (ImageView) findViewById(R.id.teamid02);
    profileImage4 = (ImageView) findViewById(R.id.teamid03);
    profileImage5 = (ImageView) findViewById(R.id.teamid04);
    profileImage6 = (ImageView) findViewById(R.id.teamid05);

    // Makes the arrow image act as a back button.
    ImageView backButton = (ImageView) findViewById(R.id.backButton);
    backButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    profileImage1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setProfilePicture(view);
        }
    });

    profileImage2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setProfilePicture(view);
        }
    });

    profileImage3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setProfilePicture(view);
        }
    });

    profileImage4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setProfilePicture(view);
        }
    });

    profileImage5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setProfilePicture(view);
        }
    });

    profileImage6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setProfilePicture(view);
        }
    });
}

public void setProfilePicture(View view) {
    //Creating a Return intent to pass to the Main Activity
    Intent returnIntent = new Intent();
    //Figuring out which image was clicked
    ImageView selectedImage = (ImageView) view;
    //Adding stuff to the return intent
    returnIntent.putExtra("imageID", selectedImage.getId());
    setResult(RESULT_OK, returnIntent);
    //Finishing Activity and return to main screen!
    finish();
}

0 个答案:

没有答案