如何防止我的活动再次发布?

时间:2017-08-30 10:51:45

标签: java android

在我的应用程序中,我有一个显示某个级别的列表视图。

我在listview中填入资产中的文件。 我的资产文件是这样的:

    <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> 

我的列表视图显示了级别,当我点击列表视图的项目时,转到另一个活动,下一个活动也会显示我的问题和我的选项。 一切都还可以,我的问题是,当第二个活动按回按钮进行第一个活动时,第一个活动再次启动,我的onCreate方法再次运行,我的listview项目增加。(例如,如果我有1个问题,点击后按回按钮显示我的问题变为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);
    }
}

}

我的loadObjects方法反复运行并运行。

这是我的第二项活动:

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再次运行它。 如何防止这种行为?

2 个答案:

答案 0 :(得分:1)

删除第startActivity(new Intent(MainActivity.this, Home.class));行,然后将通话留给finish()

答案 1 :(得分:1)

只需在AndroidManifest中添加以下行 - <activity>,其中包含ListView

android:launchMode="singleTask"

像这样,

<activity
            android:name=".MainActivity"
            android:launchMode="singleTask"/>