मैं एक छोटे और सरल एंड्रॉइड ऐप पर काम कर रहा हूं जहां मैं बटन क्लिक पर काउंटर बढ़ाता या घटाता हूं। जब ऐप onStop
चला जाता है, तो मैं टेक्स्ट फ़ाइल में वर्तमान काउंटर वैल्यू सहेज रहा हूं और जब मैं ऐप पर वापस आता हूं, तो मैं लिखित मूल्य पढ़ रहा हूं और इसे स्क्रीन पर TextView
में दिखा रहा हूं। . समस्या यह है कि जब मैं ऐप पर वापस आता हूं और मूल्य प्राप्त करता हूं और इसे बढ़ाने की कोशिश करता हूं, तो यह 0 से गिनना शुरू कर देता है। हालांकि, समस्या तब होती है जब मैं ऐप छोड़ देता हूं और onDestroy
कॉलबैक कहा जाता है, लेकिन अगर मैं फोन घुमाता हूं और उसी onDestroy
को काउंटर वर्क्स कहा जाता है क्योंकि यह काम करने की उम्मीद करता है। मेरा सवाल यह है कि नवीनतम सहेजे गए मूल्य से काउंटर को कैसे बढ़ाया जाए?
यहाँ मेरा कोड है:
मुख्य गतिविधि
public class MainActivity extends AppCompatActivity {
private static final String FILE_NAME = "test.txt";
TextView textView;
private int count;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text1);
readTextFile();
if (savedInstanceState != null){
count = savedInstanceState.getInt("count");
textView.setText(String.valueOf(count));
}
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("count", count);
}
public void decrement(View view) {
count --;
textView.setText(String.valueOf(count));
}
public void increment(View view) {
count++;
textView.setText(String.valueOf(count));
}
@Override
protected void onStop() {
super.onStop();
String text = String.valueOf(count);
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
try {
fos.write(text.getBytes());
Toast.makeText(this, "Saved to " + getFilesDir() + FILE_NAME, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
count = Integer.parseInt(text);
Log.i(TAG,"onStop");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG,"onPause");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG,"onDestroy");
}
public void readTextFile(){
FileInputStream fis = null;
try {
fis = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String text;
while ((text = br.readLine()) != null){
sb.append(text);
}
textView.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
लेआउट:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:screenOrientation="sensorLandscape"
tools:context=".MainActivity">
<TextView
android:id="@+id/text1"
android:layout_width="167dp"
android:layout_height="45dp"
android:gravity="center"
android:text="0"
android:textColor="#0C0C0C"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.603" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="decrement"
android:text="-"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="increment"
android:text="+"
app:layout_constraintBottom_toTopOf="@+id/text1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
1 उत्तर
आप count
के लिए readTextFile()
में मान सेट नहीं करते हैं, इसलिए यह 0 है। यह तब काम करता है जब आप अपने फ़ोन को घुमाते हैं क्योंकि आपने savedInstanceState
पर मान सहेजा है।
संबंधित सवाल
नए सवाल
android
एंड्रॉइड Google का मोबाइल ऑपरेटिंग सिस्टम है, जिसका उपयोग प्रोग्रामिंग या डिजिटल डिवाइस (स्मार्टफोन, टैबलेट, ऑटोमोबाइल्स, टीवी, वियर, ग्लास, IoT) को विकसित करने के लिए किया जाता है। एंड्रॉइड से संबंधित विषयों के लिए, एंड्रॉइड-विशिष्ट टैग जैसे कि एंड्रॉइड-इरादे, एंड्रॉइड-गतिविधि, एंड्रॉइड-एडॉप्टर आदि का उपयोग करें। विकास या प्रोग्रामिंग के अलावा अन्य प्रश्नों के लिए, लेकिन एंड्रॉइड फ्रेमवर्क से संबंधित हैं, इस लिंक का उपयोग करें: https: // android.stackexchange.com।