如何重新加载以前状态保存的活动?

时间:2013-04-06 15:36:21

标签: java android sqlite

我有一个游戏活动,并使用sqlite数据库,我随机填充一些按钮,没有重复,当游戏结束时,我弹出一个警告对话框,按OK按钮一些信息。现在,当我按下OK时,我需要关闭弹出窗口并在该游戏活动中将一些其他数据重新加载到我的按钮,而不重复。这个我需要发生两次。我在我的另一个游戏中执行此操作,但弹出窗口没有OK按钮,它弹出3秒钟并使用处理程序关闭,我的活动重新加载新数据而不重复,这就像一个魅力。在这个游戏中,只有OK按钮是有区别的,它不起作用。我需要OK按钮。我对此有两个问题。

  1. 我成功地重新加载了我的活动,但重复了一遍,这告诉我,我的活动从头开始。
  2. 当活动第二次加载时,当我按下后退按钮时,我可以看到之前的游戏活动,这再次告诉我,我将一次活动加载两次。我每次只需要一次新数据。 此外,我的计数器不起作用,导致第二次加载后,我得到第3,第4等等。
  3. 以下是我的游戏和弹出式活动:

    public class ToploHladno extends Activity implements View.OnClickListener{
    
        Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, bIzlaz, bDalje;
        String mButtonText1, mButtonText2, mButtonText3, mButtonText4, mButtonText5, mButtonText6,
        mButtonText7, mButtonText8, mButtonText9, mButtonText10, opis, odgovorNormalized;
        MediaPlayer buttonClicks, buttonFinal, buttonBack;
        public boolean music;
        final Context context = this;
        Editable ukucanRezultat;
        String tacanRezultat, ukucanRezultatVelikaSlova;
        int brojPoena;
        int counter = 0;
    
    
        LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();
    
        private String generateWhereClause(){
            StringBuilder result = new StringBuilder();
            for (Long l : mAnsweredQuestions){
                result.append(" AND _ID <> " + l);
            }
            return result.toString();
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.game);
    
            addListenerOnButton();
    
            nextQuestion();
        }
    
        private void addListenerOnButton() {
    
    
            b1 = (Button) findViewById(R.id.button1);
            b2 = (Button) findViewById(R.id.button2);
            b3 = (Button) findViewById(R.id.button3);
            b4 = (Button) findViewById(R.id.button4);
            bIzlaz = (Button) findViewById(R.id.bIzlaz);
            bDalje = (Button) findViewById(R.id.bDalje);
    
        }
    
        public void nextQuestion() {
    
            counter++;
    
            TestAdapter mDbHelper = new TestAdapter(this);
            mDbHelper.createDatabase();
    
            try{ 
    
                mDbHelper.open();
    
                Cursor c = mDbHelper.getTestData(generateWhereClause());
    
                mAnsweredQuestions.add(c.getLong(0));
    
                mButtonText1 = c.getString(2);
                mButtonText2 = c.getString(3);
                mButtonText3 = c.getString(4);
                mButtonText4 = c.getString(5);
    
                b1.setOnClickListener(this);
                b2.setOnClickListener(this);
                b3.setOnClickListener(this);
                b4.setOnClickListener(this);
    
    
                if(counter < 3){
    
                    b1.setText("30 poena");
                    b2.setText("25 poena");
                    b3.setText("22 poena");
                    b4.setText("20 poena");
    
                }else{
                    finish();
                }
    
            }
    
                finally{ 
                    mDbHelper.close();
                }
    
        }
    

    弹出窗口:

    public class Popup_opis extends Activity implements OnClickListener{
    
        TextView tvOpis;
        int brojPoenaPrimljeno;
        Button OK;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    
            setContentView(R.layout.popup_opis);
    
            initVariables();
    
        }
    
        private void initVariables() {
    
    
    
            OK = (Button) findViewById(R.id.bOK);
            tvOpis = (TextView) findViewById(R.id.tvOpis);
    
    
        OK.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
                finish();
                Intent intent = new Intent(Popup_opis.this, ToploHladno.class);
                //intent.putExtra("myMethod", "nextQuestion"); //I tried also this
                startActivity(intent);
    
            }
        });
        }
    
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您正在使用ToploHladno ActivityPopup_opis Activity重新启动Intent。如果ToploHladno Activity尚未完成,我认为它已经完成,那么就不需要Intent。您可以在invalidate()上致电Views,然后在onResume()上致电setText(),或者您需要对Views进行任何更改,

@Override
public void onResume()   
{
    // change your Buttons however you need in here then say you have btn1
    btn1.invalidate();
}

您的第一个Activity尚未完成,因此您不需要甚至可能想要使用Intent启动它。关闭Dialog Activity后,您将返回到该onResume()Layout将被调用,以便您可以在那里做您需要的事情。通过不创建Activity的新实例,这将使您无法重新绘制整个startActivityForResult()。请告诉我这是否适用于您需要的内容

Invalidate

Intent intent = new Intent(ToploHladno .this, Popup_opis .class); intent.putExtra("counter", counter); startActivityForResult( intent, 0 ); //second param is a request code. You will need this later 示例:

public class Popup_opis extends Activity implements OnClickListener{

TextView tvOpis;
int brojPoenaPrimljeno;
Button OK;
int counter;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.popup_opis);

        //Get intent
        Intent recIntent = getIntent();
         counter = recIntent.getIntExtra("counter");   //get counter variable from previous activity

OK.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
       counter++;

        Intent intent = new Intent(Popup_opis.this, ToploHladno.class);
        intent.putExtra("counter", counter); //SEND BACK COUNTER
        setResult(RESULT_OK, intent);  // send result
finish();

然后在弹出活动中

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
// would use REQUEST CODE sent earlier if more than one activity sends back intents here
    if (resultCode == RESULT_OK)
    {
        counter = data.getInteExtra("counter", 0);  // get updated counter variable
    }
}

然后在您之前的活动中

{{1}}

More about startActivityForResult in Docs