gpt4 book ai didi

java - 如何重新启动相同的 Activity ?

转载 作者:太空宇宙 更新时间:2023-11-04 14:41:41 26 4
gpt4 key购买 nike

如果我有 10 个 EditText 并且用户仅输入 5 个,那么我在单击 onBackPressed 按钮时退出我的应用程序,而不提交正在输入的 EditText 数据,然后当我重新启动我的应用程序时,我希望在启动时进行相同的 Activity 。感谢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();
}

}
});
}

最佳答案

你可以简单地使用这个

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

我希望这有效

关于java - 如何重新启动相同的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24861126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com