2014-07-21 47 views
1

如果我有10 EditText和用戶只輸入5,然後我退出我的應用程序上單擊onBackPressed按鈕,而不提交EditText數據輸入,然後當我重新啓動我的應用程序我想要啓動相同的活動。謝謝去appriceat。如何重新啓動相同的活動?

public class Registration_Form extends Activity { 
EditText et_CompanyName; 
EditText et_EmployeeName; 
EditText et_CompanyWebsite; 
EditText et_ContactNumber; 
EditText et_Email_Id; 

Button btnSubmit; 
DatabaseHelper databaseHelper; 
SQLiteDatabase db; 

RadioGroup radioGroup_FinancialYaer; 
RadioButton radioButton_FinancialYaer; 
String strFinancialYear; 
String appWidgetId = null; 


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

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    final SharedPreferences.Editor edit = prefs.edit(); 
    boolean alldataSaved=prefs.getBoolean("SecondRun",false); 

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



    et_CompanyName.setOnFocusChangeListener(new OnFocusChangeListener() {   

     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus) 
      { 
       edit.putString("Company_Name"+appWidgetId,et_CompanyName.getText().toString()); 
       edit.commit(); 
      } 
     } 
    }); 


    et_EmployeeName.setOnFocusChangeListener(new OnFocusChangeListener() {   

     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus) 
      { 
       edit.putString("Employee_Name"+appWidgetId,et_EmployeeName.getText().toString()); 
       edit.commit(); 
      } 
     } 
    }); 



    et_CompanyWebsite.setOnFocusChangeListener(new OnFocusChangeListener() {   

     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus) 
      { 
       edit.putString("Company_Website"+appWidgetId,et_CompanyWebsite.getText().toString()); 
       edit.commit(); 
      } 
     } 
    }); 


    et_ContactNumber.setOnFocusChangeListener(new OnFocusChangeListener() {   

     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus) 
      { 
       edit.putString("Contact_Number"+appWidgetId,et_ContactNumber.getText().toString()); 
       edit.commit(); 
      } 
     } 
    }); 



    et_Email_Id.setOnFocusChangeListener(new OnFocusChangeListener() {   

     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus) 
      { 
       edit.putString("Email_Id"+appWidgetId,et_Email_Id.getText().toString()); 
       edit.commit(); 
      } 
     } 
    }); 






     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 
     { 
      if(alldataSaved == false) 
      { 
       SharedPreferences.Editor editor=prefs.edit(); 
       editor.putBoolean("SecondRun",true); 
       editor.commit(); 
       Log.e("Second"," Steps !!!!"); 
      } 
      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); 


    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(); 
      } 

     } 
    }); 
} 
+0

你希望當你重新啓動應用程序時那四個edittext應該被填充? –

+0

使用SharedPreferences .. – Kedarnath

+0

@Kalpesh Lakhani:對。 – jvd

回答

0

您可以爲此使用sharedPreference。你可以保存在一個sharedprefrerece每個EditText上的值(爲每個的EditText和設定值在焦點變化時唯一鍵。見例如:)

EditText txtEdit= (EditText) findViewById(R.id.edittxt); 

txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {   

    public void onFocusChange(View v, boolean hasFocus) { 
     if(!hasFocus) 
      //do job here when Edittext lose focus 
      //means save value here 
    } 
}); 

,並在上加載這些保存的價值創造。 希望你從這個

編輯得到一些想法:這是你的條件

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 
     {  
if(alldataSaved==false) 
    { 
     //stay in this activiy 

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

但如何來相同的活動。在第二次啓動應用程序後,我已經使用共享首選項來總是來第二個Activity。 – jvd

+0

好了解了你的問題......做一件事把其他條件放在那裏,例如: 1)另一個sharedpref檢查是否所有的edittext值都保存在sharedpref中。 2)如果所有值都沒有保存,那麼保持相同的活動,否則不保留。 –

+0

如何留在這個相同的活動。 – jvd

0

,你可以簡單地使用這個

Intent intent = getIntent(); 
finish(); 
startActivity(intent); 

我希望這就是工作

0

這而不是Android的工作方式。你不能以這種方式「恢復」同一個活動。但是,您可以保存EditText內容,例如在onPause()或onStop()函數中,將EditTexts的內容保存爲字符串,將它們放入列表中,然後將該列表序列化到文件中。在onResume()或onStart()方法中,從文件反序列化這些數據。

在這裏顯示了這種序列化的一個例子,但我會在這裏也貼: Put objects into bundle

連載:

private List<String> list = new ArrayList<String>(); 

@Override 
public void onPause() 
{ 
    super.onPause(); 
    FileOutputStream out = null; 
    try 
    { 
     out = openFileOutput("ModelBackup",Context.MODE_PRIVATE); 

     try 
     { 
      ObjectOutputStream oos = new ObjectOutputStream(out); 
      oos.writeObject(list); 
     } 
     catch(IOException e) 
     { 
      Log.d(this.getClass().toString(), e.getMessage()); 
     } 
    } 
    catch(FileNotFoundException e) 
    { 
     Log.d(this.getClass().toString(), e.getMessage()); 
    } 
    finally 
    { 
     try 
     { 
      if(out != null) out.close(); 
     } 
     catch(IOException e) 
     { 
      Log.d(this.getClass().toString(), e.getMessage()); 
     } 
    } 
} 

反序列化:

@Override 
public void onResume() 
{ 
    super.onResume(); 
    FileInputStream in = null; 
    try 
    { 
     in = openFileInput("ModelBackup"); 
     ObjectInputStream oos = new ObjectInputStream(in); 
     try 
     { 
      list = (List<String>)oos.readObject(); 
     } 
     catch(ClassNotFoundException e) 
     { 
      list = null; 
     } 
    } 
    catch(IOException e) 
    { 
     Log.d(this.getClass().toString(), e.getMessage()); 
    } 
    finally 
    { 
     try 
     { 
      if(in != null) in.close(); 
     } 
     catch(IOException e) {} 
    } 
} 
0

如果重新啓動活動從一個片段,我會這樣做:

new Handler().post(new Runnable() { 

     @Override 
     public void run() 
     { 
      Intent intent = getActivity().getIntent(); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION); 
      getActivity().overridePendingTransition(0, 0); 
      getActivity().finish(); 

      getActivity().overridePendingTransition(0, 0); 
      startActivity(intent); 
     } 
    });