私のアプリケーションでは、いくつかのレベルを示すリストビューがあります。アクティビティが再び起動されないようにするにはどうすればよいですか?
リストビューにはアセットのファイルが入っています。 資産の私のファイルは、このようなものです:
<question>
<image>pic1.png</image>
<option1>one.png</option1>
<option2>two.png</option2>
<option3>three.png</option3>
<option4>four.png</option4>
<answer>1</answer>
<level>1</level>
</question>
私のリストビューには、レベルを示し、私は別のアクティビティに移動リストビューの項目をクリックすると、次のアクティビティにだけでなく、私の質問と私のオプションを示しています。 すべてのことはOKですが、私の問題は、2番目のアクティビティに戻るボタンを押して最初のアクティビティに移動すると、最初のアクティビティが再び起動し、onCreateメソッドが再び実行され、リストビューのアイテムが増加します。私の質問が2)になることを示す戻るボタンを押してください。
public class Home extends Activity {
static SharedPreferences prefs;
Editor editor;
HorizontalListView hListView;
MyArrayAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
loadObjects();
prefs = getSharedPreferences(DataStore.MY_PREFS_NAME, MODE_PRIVATE);
editor = prefs.edit();
editor.putInt("l" + 1, Question.KEY_UNLOCK);
editor.commit();
hListView = (HorizontalListView) findViewById(R.id.list_view);
listAdapter = new MyArrayAdapter(this, DataStore.questions);
hListView.setAdapter(listAdapter);
hListView.scrollTo(prefs.getInt("level", 0));
hListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
int lock = prefs.getInt("l" + DataStore.questions.get(index).level, Question.KEY_LOCK);
if (lock == Question.KEY_LOCK)
showLockAction();
else {
Intent intent = new Intent(Home.this, MainActivity.class);
intent.putExtra("selected_level", index);
startActivity(intent);
}
}
});
}
protected void showLockAction() {
Toast.makeText(Home.this, "مرحله های قبل و کامل کن", Toast.LENGTH_LONG).show();
}
public void loadObjects() {
String KEY_QUESTION = "question";
String KEY_OPTION1 = "option1";
String KEY_OPTION2 = "option2";
String KEY_OPTION3 = "option3";
String KEY_OPTION4 = "option4";
String KEY_QUESTION_IMAGE = "image";
String KEY_LOCK = "level";
String KEY_ANSWER = "answer";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromAssets(Home.this);
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_QUESTION);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
Question q = new Question();
q.image = parser.getValue(e, KEY_QUESTION_IMAGE);
q.options.add(parser.getValue(e, KEY_OPTION1));
q.options.add(parser.getValue(e, KEY_OPTION2));
q.options.add(parser.getValue(e, KEY_OPTION3));
q.options.add(parser.getValue(e, KEY_OPTION4));
q.level = Integer.parseInt(parser.getValue(e, KEY_LOCK));
q.answer = Integer.parseInt(parser.getValue(e, KEY_ANSWER));
DataStore.questions.add(q);
}
}
}
私のロードオブジェクトのメソッドの実行と繰り返し実行します。
は、ここで私の最初の活動です。
とここで私の第二の活動です:
public class MainActivity extends Activity {
ImageView questionText;
TextView level;
ImageView option1;
ImageView option2;
ImageView option3;
ImageView option4;
ImageView dialog;
Button nextLevel;
LinearLayout mainLayout;
Question curQuestion;
int curQuestionIndex = 0;
int wrongCounter = 1;
SharedPreferences prefs;
Editor editor;
ArrayList<Integer> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mainLayout = (LinearLayout) findViewById(R.id.main_lay);
dialog = (ImageView) findViewById(R.id.dialog);
level = (TextView) findViewById(R.id.level);
nextLevel = (Button) findViewById(R.id.next_level);
nextLevel.setOnClickListener(nextLevelClick);
questionText = (ImageView) findViewById(R.id.question);
option1 = (ImageView) findViewById(R.id.option1);
option2 = (ImageView) findViewById(R.id.option2);
option3 = (ImageView) findViewById(R.id.option3);
option4 = (ImageView) findViewById(R.id.option4);
option1.setOnClickListener(clickListener);
option2.setOnClickListener(clickListener);
option3.setOnClickListener(clickListener);
option4.setOnClickListener(clickListener);
prefs = getSharedPreferences(DataStore.MY_PREFS_NAME, MODE_PRIVATE);
editor = prefs.edit();
curQuestionIndex = getIntent().getIntExtra("selected_level", 0);
loadNewQuestion();
}
OnClickListener nextLevelClick = new OnClickListener() {
@Override
public void onClick(View v) {
stopCorrectAnswerAction();
curQuestionIndex++;
loadNewQuestion();
}
};
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (curQuestion.isAnswer(v.getId())) {
correctAnswerAction();
} else
wrongAnswerAction();
}
};
private void loadNewQuestion() {
Log.e("load", "@------loading------> " + curQuestionIndex);
if (curQuestionIndex == DataStore.questions.size()) {
gameFinishedAction();
curQuestionIndex = 0;
return;
}
curQuestion = DataStore.questions.get(curQuestionIndex);
editor.putInt("level", curQuestionIndex + 1);
editor.putInt("l" + curQuestion.level, Question.KEY_UNLOCK);
editor.commit();
wrongCounter = 1;
level.setText("مرحله " + curQuestion.level);
dialog.setImageResource(R.drawable.cl_q);
questionText.setImageBitmap(getBitmapFromAsset(curQuestion.image));
option1.setImageBitmap(getBitmapFromAsset(curQuestion.options.get(0)));
option2.setImageBitmap(getBitmapFromAsset(curQuestion.options.get(1)));
option3.setImageBitmap(getBitmapFromAsset(curQuestion.options.get(2)));
option4.setImageBitmap(getBitmapFromAsset(curQuestion.options.get(3)));
}
private void gameFinishedAction() {
// TODO Auto-generated method stub
}
protected void wrongAnswerAction() {
if (wrongCounter % 3 == 1)
dialog.setImageResource(R.drawable.cl_w1);
else if (wrongCounter % 3 == 2)
dialog.setImageResource(R.drawable.cl_w2);
else if (wrongCounter % 3 == 0)
dialog.setImageResource(R.drawable.cl_w3);
wrongCounter++;
}
protected void correctAnswerAction() {
//mainLayout.setAlpha(0.5f);
dialog.setImageResource(R.drawable.cl_c);
nextLevel.setVisibility(View.VISIBLE);
option1.setOnClickListener(null);
option2.setOnClickListener(null);
option3.setOnClickListener(null);
option4.setOnClickListener(null);
//Toast.makeText(getApplicationContext(), "درسته",Toast.LENGTH_LONG).show();
}
protected void stopCorrectAnswerAction() {
//mainLayout.setAlpha(1);
nextLevel.setVisibility(View.INVISIBLE);
option1.setOnClickListener(clickListener);
option2.setOnClickListener(clickListener);
option3.setOnClickListener(clickListener);
option4.setOnClickListener(clickListener);
}
private Bitmap getBitmapFromAsset(String strName) {
AssetManager assetManager = MainActivity.this.getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
@Override
public void onBackPressed() {
startActivity(new Intent(MainActivity.this, Home.class));
this.finish();
}
私はこの方法を行う
@Override
public void onBackPressed() {
startActivity(new Intent(MainActivity.this, Home.class));
this.finish();
}
私の最初の活動は再びそれを実行するのonCreate。 この操作を防止するにはどうすればよいですか?
を私はそのアクティビティを開いたときに 'onBackPressed'と' Home'アクティビティをスタックから開放します。 –
私は、 ain – user7908469