Android中的恢复方法

时间:2014-07-21 07:05:31

标签: java android

如果我没有完整填写表单并单击提交按钮并退出我的活动,我想要恢复相同的活动。再次我启动我的应用程序我想要开始的同样的活动。如何做到这一点。可以帮我一些拜托。谢谢。

这是我的代码。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration_details);

          SharedPreferences
          settings=getSharedPreferences("prefs",0); boolean
          firstRun=settings.getBoolean("firstRun",false);

          if(firstRun==false)//if running for first time 

          {
              SharedPreferences.Editor editor=settings.edit();
              editor.putBoolean("firstRun",true); 
              editor.commit();
              //execute your code for first time
          } 

          else 
          {     
              Intent iSubmit = new Intent(Registration_Form.this,Employee_List.class);
              startActivity(iSubmit);
              finish();
              //Default Activity startActivity(a); 
          }

        databaseHelper = new DatabaseHelper(this);
        databaseHelper.onOpen(db);

        et_CompanyName = (EditText) findViewById(R.id.editText_CompanyName);
        et_EmployeeName = (EditText) findViewById(R.id.editText_EmployeeName);
        et_CompanyWebsite = (EditText) findViewById(R.id.editText_CompanyWebSite);
        et_ContactNumber = (EditText) findViewById(R.id.editText_ConatctNo);
        et_Email_Id = (EditText) findViewById(R.id.editText_EmailId);

        radioGroup_FinancialYaer = (RadioGroup)findViewById(R.id.radioGroupFinanncialYear);


        btnSubmit = (Button) findViewById(R.id.buttonSubmit);
        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                final String stringEmail_Id = et_Email_Id.getText().toString()
                        .trim();

                final String stringCompanyWebsite = et_CompanyWebsite.getText()
                        .toString().trim();

                if ((et_CompanyName.getText().toString().isEmpty())) {
                    et_CompanyName.setError("Field Can Not Be Empty !");
                }

                else if (!et_CompanyName.getText().toString().trim()
                        .matches("[a-zA-Z ]+")) {
                    et_CompanyName.setError("Accept Alphabets Only.");
                }

                else if ((et_EmployeeName.getText().toString().isEmpty())) {
                    et_EmployeeName.setError("Field Can Not Be Empty !");
                }

                else if (!et_EmployeeName.getText().toString().trim()
                        .matches("[a-zA-Z ]+")) {
                    et_EmployeeName.setError("Accept Alphabets Only.");
                }

                else if ((et_CompanyWebsite.getText().toString().isEmpty())) {
                    et_CompanyWebsite.setError("Field Can Not Be Empty !");
                }

                else if (!isValidUrl(stringCompanyWebsite)) {
                    et_CompanyWebsite.setError("Invalid URL");
                }

                else if ((et_ContactNumber.getText().toString().isEmpty())) {
                    et_ContactNumber.setError("Field Can Not Be Empty !");
                }

                else if (!isValidEmail(stringEmail_Id)) {
                    et_Email_Id.setError("Invalid Email");
                }

                else

                {
                    String stringCompanyName = et_CompanyName.getText()
                            .toString().trim();
                    String stringContactNumber = et_ContactNumber.getText()
                            .toString().trim();
                    String stringEmployeeName = et_EmployeeName.getText()
                            .toString().trim();


                    int selectedId = radioGroup_FinancialYaer.getCheckedRadioButtonId();
                    Log.e("selectedId "," = " + selectedId);
                    radioButton_FinancialYaer = (RadioButton) findViewById(selectedId);
                    strFinancialYear = radioButton_FinancialYaer.getText().toString().trim();
                    Log.e("strRadioButton "," = " + strFinancialYear);


                    databaseHelper.insertRegstrationDetails(stringCompanyName,
                            stringEmployeeName, stringCompanyWebsite,
                            stringContactNumber, stringEmail_Id, strFinancialYear);
                    System.out.println("Data Inserted Successfully !!! ");

                    Intent iSubmit = new Intent(Registration_Form.this,Staff_Employee_Details.class);
                    startActivity(iSubmit);
                    finish();
                }

            }
        });
    }

    // validating email id
    private boolean isValidEmail(String email) {

        String EMAIL_PATTERN = "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@"
                + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
                + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+";

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }


    private boolean isValidUrl(String url) {
        Pattern p = Patterns.WEB_URL;
        Matcher m = p.matcher(url);
        if(m.matches())
            return true;
        else
        return false;
    }

    @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         if ((keyCode == KeyEvent.KEYCODE_BACK)) {
             AlertDialog.Builder alertbox = new AlertDialog.Builder(Registration_Form.this);

             alertbox.setTitle("Do you wish to exit ?");
             alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface arg0, int arg1) { 
                    // finish used for destroyed activity
                     finish();
                 }
             });

             alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface arg0, int arg1) {
                         // Nothing will be happened when clicked on no button 
                         // of Dialog     
               }
             });

             alertbox.show();
         }
         return super.onKeyDown(keyCode, event);
     }

}

1 个答案:

答案 0 :(得分:0)

将所有EditText值放在Database

的表格中

在表格中保留一个名为count的额外列。当用户按Submit按钮时,根据用户在EditText中输入的条目数量增加count值,并将其保存在数据库中。

当用户再次启动您的应用时,请检查计数值,如果它等于您的预期值,则根据您的要求路由他,如果它低于预期,则显示您的表单活动,从数据库填充数据。

您可以使用启动画面来实现此功能。

编辑:您实际想要的是什么

如果用户按下后退按钮并启动您的应用程序,如果它是您的启动器活动,您将被定向到相同的活动,否则您可以检查是否所有字段都没有填写,然后您可以保存{ {1}}中的值{1}}并在启动您的应用时检查其状态,如果确实如此,则启动此活动。