我有一个包含两个活动的应用程序。当我切换到第二个活动时,数字是从 firebase 数据库中获取的,基于填充的进度条位于片段中。我的问题是,当我进入活动时,进度条没有填满,但如果我按下系统后退按钮(这将使我们返回到上一个活动),然后再次启动活动,进度条就会填满!我完全不知道为什么会这样。进度条填充在片段的 onCreateView() 中,对象是在活动的 onCreate() 中从数据库中读取的。重新创建片段时,没有任何变化,由此我得出结论,问题出在从数据库加载时。但是为什么会出现这个问题呢,因为我第二次去有fragment的activity的时候,执行的是和第一次执行的代码是一样的!
我真诚地希望得到你的支持,因为我不知道该怎么办了。
onCreate()方法代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
auth = FirebaseAuth.getInstance();
user = auth.getCurrentUser();
if (user != null) {
// already signed in
uid = user.getUid();
name = user.getDisplayName();
email = user.getEmail();
} else {
// not signed in
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setProviders(Arrays.asList(new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build()//,
// new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build()
// new AuthUI.IdpConfig.Builder(AuthUI.TWITTER_PROVIDER).build()
))
.build(),
200);
}
database = FirebaseDatabase.getInstance();
if (uid != null) {
charRef = database.getReference("Users").child(uid).child("Character");
charRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
myCharacter = dataSnapshot.getValue(Runner.class);
SnickNameBottom = myCharacter.getNickname();
currentSpeed = myCharacter.getSpeed();
currentStr = myCharacter.getStrength();
currentStamina = myCharacter.getStamina();
currentIntuition = myCharacter.getIntuition();
SlevelBottom = getString(R.string.level) + myCharacter.getCurrentLevel();
nickNameBottom = (TextView) findViewById(R.id.nickNameBottom);
levelBottom = (TextView) findViewById(R.id.levelBottom);
nickNameBottom.setText(SnickNameBottom);
levelBottom.setText(SlevelBottom);
stamFromEq = myCharacter.getStaminaFromEquipment();
strFromEq = myCharacter.getStrengthFromEquipment();
spdFromEq = myCharacter.getSpeedFromEquiment();
winsInARow = myCharacter.getWinsInARow();
wins = myCharacter.getWins();
defeats = myCharacter.getDefeats();
tournamentsTotal = myCharacter.getTournamentsTotal();
winsInTournaments = myCharacter.getWinsInTournaments();
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.container, new CardFrontFragment())
.commit();
}
}
片段 onCreateView() 方法代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.card_front, container, false);
nickNameBottom = (TextView) view.findViewById(R.id.nickNameBottom);
levelBottom = (TextView) view.findViewById(R.id.levelBottom);
progressStr = (RoundCornerProgressBar) view.findViewById(R.id.strengthbar);
progressSpd = (RoundCornerProgressBar) view.findViewById(R.id.speedbar);
progressStamina = (RoundCornerProgressBar) view.findViewById(R.id.staminabar);
progressIntuition = (RoundCornerProgressBar) view.findViewById(R.id.intuitionBar);
nickNameBottom.setText(SnickNameBottom);
levelBottom.setText(SlevelBottom);
statsSum = (float) currentSpeed + currentStamina + currentStr;
progressStr.setMax(statsSum);
progressStr.setProgress((float)currentStr);
progressStr.setSecondaryProgress((float) strFromEq);
progressSpd.setMax(statsSum);
progressSpd.setProgress((float)currentSpeed);
progressSpd.setSecondaryProgress((float)spdFromEq);
progressStamina.setMax(statsSum);
progressStamina.setProgress((float)currentStamina);
progressStamina.setSecondaryProgress((float)stamFromEq);
progressIntuition.setMax(statsSum);
progressIntuition.setProgress((float) currentIntuition);
TextView totalPlayed = (TextView) view.findViewById(R.id.total);
totalPlayed.setText(getString(R.string.total) + " " + (wins + defeats));
TextView winsT = (TextView) view.findViewById(R.id.wins);
winsT.setText(getString(R.string.wins) + " " + wins);
TextView defeatsT = (TextView) view.findViewById(R.id.defeats);
defeatsT.setText(getString(R.string.defeats) + " " + defeats);
TextView tournamentsTotalT = (TextView) view.findViewById(R.id.tournamentsTotal);
tournamentsTotalT.setText(getString(R.string.tournamentsTotal) + " " + tournamentsTotal);
TextView tournamentsWinsT = (TextView) view.findViewById(R.id.tournamentsWins);
tournamentsWinsT.setText(getString(R.string.tournamentsWins) + " " + winsInTournaments);
TextView winsInARowT = (TextView) view.findViewById(R.id.winsInARow);
winsInARowT.setText(getString(R.string.winsInARow) + " " + winsInARow);
return view;
进行了一项实验,启动了活动,关闭了互联网,按下并重新启动 - 一切都被填满了。所以所有的数据都已经从第一次加载了。
这是一段视频,清楚地展示了一切。
调用 onCreateView 片段后,您的数据就可以收到了。我不太明白你的数据是如何从活动传输到片段的,我建议在片段中加载数据或者以某种方式通知它数据已经加载。例如在一个片段中:
在活动中:
尝试在 super.onViewCreated() 之后在片段的 onViewCreated() 方法中设置数据;