[英]New arraylist created automatically when fragment is put into view
我正在創建一個應用程序,用戶可以在其中查看他們的分數歷史記錄,它工作正常,除了每當我直接單擊片段時,都會使用以前的自定義項自動創建一個新的 arraylist 列表。 當我使用保存按鈕意圖訪問片段時,不會出現此類問題,並且 recyclerview 會按照我的意願進行更新。
這就是我使用導航菜單項保存分數的方式
case R.id.save_game:
Intent save = new Intent(getApplicationContext(), ScoreActivity.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_DATE", format);
save.putExtras(extras);
startActivity(save);
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
break;
我在我的 ScoreFragment 中收到了意圖
public void saveScore(){
Intent intent = requireActivity().getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
String format = extras.getString("EXTRA_DATE");
mScoreList.add(new ScoreItem(TeamAName, TeamBName, scoreTeamA, scoreTeamB, format));
saveData(); //Saves Arraylist to sharedpreferences
}
}
然后我在我的 oncreateView 方法中調用saveScore()
我的分數片段像這樣附加到 ScoreActivity
public class ScoreActivity extends AppCompatActivity {
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.container,
new ScoreFragment()).commit();
}
}
}
我嘗試將我的saveScore()
方法放在onViewCreated()
以及onResume
和onSaveInstanceState
中,但它沒有改變任何東西。
在我的 ScoreActivity 中將replace()
更改為add()
也無濟於事。
在我膨脹我的 rootView 之后,我立即調用loadData()
並且我一直試圖在其他地方調用它,但它會導致崩潰。
private void loadData() {
SharedPreferences sharedPreferences = requireActivity().getSharedPreferences("shared preferences", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<ScoreItem>>() {
}.getType();
mScoreList = gson.fromJson(json, type);
if (mScoreList == null) {
mScoreList = new ArrayList<>();
}
}
我該如何解決這個問題?
getIntent()
始終返回您用於創建該 Activity 的 Intent - 因此,如果您使用該菜單項來啟動一個新菜單項並添加額外的
Intent save = new Intent(getApplicationContext(), ScoreActivity.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_DATE", format);
save.putExtras(extras);
startActivity(save);
那么當您調用getIntent
時,該 Activity 將始終返回此 Intent + Extras 包。 因此,如果您的片段在其onViewCreated
中有這個:
Intent intent = requireActivity().getIntent();
Bundle extras = intent.getExtras();
並且它的父活動最初是使用 Intent+Extras 組合創建的,然后它將運行您的添加代碼
老實說,這取決於您的應用程序的設置方式,因此我只能為您提供要查看的內容的想法
如果您有一個片段被交換的活動,那么ScoreFragment
可以重新添加(您提到“使用導航抽屜導航到片段”)然后onViewCreated
將使用該 Intent 再次運行。
如果它在屏幕外並且系統銷毀它的視圖以保存 memory,當它重新創建時,它將使用該 Intent 再次運行onViewCreated
。
如果您在不同的地方調用startActivity
,但現有的 Activity 被重用(比如您的 Activity 作為singleTop 或 singleInstance啟動),那么它將具有最初創建時使用的 Intent+Extras。 因此,如果您首先通過帶有附加功能的菜單項創建它,然后從其他地方調用沒有附加功能的startActivity()
,如果原始活動被使用,它將仍然具有創建時使用的 Intent+Extras。 (它將通過onNewIntent()
接收新意圖,但不會改變getIntent
的值)
此外,您實際上並沒有檢查 extras Bundle 是否具有EXTRA_DATE
項目,只是它有一些附加項,然后您添加了一個新的ScoreItem
,其中包含extras.getString("EXTRA_DATE")
返回的任何內容(可能是 `null` `)。 因此,如果您在不同情況下與其他捆綁包一起啟動,您可能仍會獲得額外的分數
我知道這很多,這不是一個解決方案,但希望它有所幫助! 老實說,我建議在saveScore
中設置一個斷點,調試應用程序並使用調試器查看getIntent
為您提供了什么,以及它是否正確。
所以我設法解決了這個問題,但我不知道我到底做了什么。 我決定為我的應用程序創建一個簡單的啟動畫面,問題就消失了。 我現在很困惑,我需要一些幫助來解決這個問題,以便將來糾正它。
我創建了一個名為 SplashScreenActivity 的新活動
public class SplashScreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EasySplashScreen config = new EasySplashScreen(SplashScreenActivity.this)
.withFullScreen()
.withTargetActivity(MainActivity.class)
.withSplashTimeOut(5000)
.withBackgroundColor(Color.parseColor("#FF9800"))
.withHeaderText("Header")
.withFooterText("Footer")
.withBeforeLogoText("Before Logo Text")
.withAfterLogoText("After Logo Text")
.withLogo(R.mipmap.ic_launcher_round);
config.getHeaderTextView().setTextColor(Color.WHITE);
config.getFooterTextView().setTextColor(Color.WHITE);
config.getBeforeLogoTextView().setTextColor(Color.WHITE);
config.getAfterLogoTextView().setTextColor(Color.WHITE);
View easySplashScreen = config.create();
setContentView(easySplashScreen);
}
}
然后我將 MainActivity 的意圖過濾器移至 Manifest.xml 文件中的啟動屏幕活動,這就是我認為改變的地方。
<activity android:name=".SplashScreenActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我將它移回 MainActivity 幾次,我意識到每次 Intent 過濾器位於 MainActivity 時都會出現問題。 現在我將它保存在清單中的.SplashScreenActivity
中,我沒有這樣的問題。 我真的需要一些解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.