How to close an activity at launch in Android
In my app I have two activities. At the time of launch I am showing an edit text box and a button in the first activity. When the button is clicked, it moves to the next activity.
Before that, when the edit box is typed, it gets stored in a database. When the activity gets started it checks over the database. If the database is empty it shows the first activity else if the database is filled I want to show the second activity directly. But it is not happening in my code. Please anyone help me? I want to know where I am going wrong…
super.onCreate(savedInstanceState);
db = openOrCreateDatabase("TestData.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
final String CREATE_TABLE_COUNTRIES ="CREATE TABLE IF NOT EXISTS tbl_countries (id TEXT);";
db.execSQL(CREATE_TABLE_COUNTRIES);
ContentValues values = new ContentValues();
values.put("id", textvalue );
Cursor cx = db.rawQuery("select id from tbl_countries" , null);
counter=0;
if (cx != null )
{
if (cx.moveToFirst())
{
do
{
counter=counter+1;
}
while (cx.moveToNext());
}
}
db.close();
if(counter == 0)
{
setContentView(R.layout.main);
et = (EditText)findViewById(R.id.editText1);
textvalue = et.getText().toString();
b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent myIntent = new Intent(getBaseContext(), SeconDClass.class);
startActivityForResult(myIntent, 0);
finish();
}
});
}
else if(counter>=1)
{
Intent myIntent = new Intent开发者_如何学JAVA(getBaseContext(), SeconDClass.class);
startActivityForResult(myIntent, 0);
finish();
}
I think it might be because of this
startActivityForResult(myIntent, 0);
finish();
It doesn't make sense to finish the activity that would be receiving the result.
try just startActivity()
, you don't really need to call finish()
the first one.
精彩评论