Java – always return to the login screen

I have an application that starts the splash activity screen for 5 seconds, then opens the login activity screen, then opens the listactivity after you enter the correct user and password, and then click on each line to open the mycity activity

to update:

What I want is: you are anywhere in my app, you leave my app for any reason, not only when you press the home button, but also for exploits:

>You press the home button to check another application and then want to return to my application. > You have a notification about WhatsUp or email, display a new message, you open your WhatsUp or email, and then return to my application

3 - you want to check my app after you leave your phone for a while

4 - press the power button to turn off the phone (lock the screen), then turn on the lock and want to return to my application

I mean, any time you leave my app for any reason without pressing the back button, it will exit the whole app, and then if you want to return to my app again, you must open your login screen and enter your user name and password again

I called (); For splash and login activities

I tried Android: cleartaskonlaunch = "true" in the login activity in the list, but it didn't

Any suggestions will be seriously considered,

Please write the complete working code

Login activity:

public class Login extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    Button b = (Button) findViewById(R.id.loginbutton);

  b.setOnClickListener(new OnClickListener() {

  public void onClick(View v) {

    EditText username = (EditText) findViewById(R.id.login);
    EditText password = (EditText) findViewById(R.id.password);

     if(username.getText().toString().length() > 0 && password.getText().
             toString().length() > 0 ) {
if(username.getText().toString().equals("test") && password.getText().
             toString().equals("test")) {


    Intent intent = new Intent(Login.this,Menu.class);
    startActivity(intent);
       finish(); }
            }   }               
                    });  }  }

Menu activity:

public class Menu extends ListActivity {

      String classes[] = { "City1","City2","City3","City4","City5"};

          @Override
      protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         super.onCreate(savedInstanceState);

      setlistadapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classes));
                                   }

     @Override
       protected void onListItemClick(ListView l,View v,int position,long id) {
         // TODO Auto-generated method stub
          super.onListItemClick(l,v,position,id);
         String cheese = classes[position];
   try {
    Class ourClass = Class.forName("com.test.demo.MyCity");
    Intent ourIntent = new Intent(Menu.this,ourClass);
    ourIntent.putExtra("cheese",cheese);
         startActivity(ourIntent);
           } catch (ClassNotFoundException e) {
        e.printStackTrace();
                            }
                    }}

Mycity activity:

public class MyCity extends Activity {
TextView tv1;
String city;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.city);  

    initializeTextViews();}

private void initializeTextViews() {

    tv1=(TextView)findViewById(R.id.city_tv);
     city=getIntent().getStringExtra("cheese");

if(city.equalsIgnoreCase("City1")){

        tv1.setText(Html.fromHtml(getString(R.string.city1)));}


    else if(city.equalsIgnoreCase("City2")){

        tv1.setText(Html.fromHtml(getString(R.string.city2)));}


    else if(city.equalsIgnoreCase("City3")){

        tv1.setText(Html.fromHtml(getString(R.string.city3)));}


    else if(city.equalsIgnoreCase("City4")){

        tv1.setText(Html.fromHtml(getString(R.string.city4)));}


    else if(city.equalsIgnoreCase("City5")){

        tv1.setText(Html.fromHtml(getString(R.string.city5)));}


    }}

My performance:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".Splash"
            android:label="@string/app_name">   
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Login"
            android:label="@string/app_name" 
                 android:clearTaskOnLaunch="true">
            <intent-filter>
                <action android:name="com.test.demo.LOGIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.test.demo.MENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

              <activity
            android:name=".MyCity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.test.demo.MYCITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Update 2: I have reached half of what I want, but there are still some steps I can't achieve. It is explained as follows:

By applying Android: cleartaskonlaunch = "true" to the splash activity,

And prevent the back button behavior on the menu activity:

@Override
    public boolean onKeyDown(int keyCode,KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
       moveTaskToBack(true);
       return true;
            }
    return super.onKeyDown(keyCode,event);
                    } }

So now press the home button to leave my app and return to my app:

Enter the login activity directly

But now the main objectives are:

If:

The screen locks when you leave the phone, or tap the power button to lock the phone

or

Messages opened in notifications

or

Open email notification

or

You have a call and answer it,

Then return to my application. It will not enter the login activity, but you will return to your page

Please make any suggestions, thank you

Latest news:

I use another code to override the home button and control the back button instead of applying: Android: cleartaskonlaunch = "true" to the splash activity in the list. Just apply the following code to the menu activity:

@Override
  public boolean onKeyDown(int keyCode,KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
       moveTaskToBack(true);
         return true;}

    else if (keyCode == KeyEvent.KEYCODE_HOME) { 
     Intent i=new Intent(Menu.this,Login.class);
       startActivity(i);
          finish();
        return true;}
    return super.onKeyDown(keyCode,event);}

Solution

Here is the solution I came up with.

Please download the project at the end of the blog post and test it

Test:

>Samsung S3 runs Android 4.0 4 > the simulator runs Android 2.3 one

Basic idea: we will create a requireliginactivity. All our activities will extend it except loginactivity

Three situations should be caught when calling the onresume function:

>Use the flavor of startactivity to jump from one requireliginactivity to another. > By completing the current activity, jump back from a requireliginactivity to the previous requireliginactivity. > Return to requireliginactivity after hiding (we should show login here!)

The basic idea of my solution is to have two counters: the number of started activities (startcounter) and the number of suspended activities (pausecounter) We will add startcounter at the beginning of each activity Similarly, pausecounter should be incremented when the activity is paused In our onresume function, we will determine whether to enter the login by comparing the two counters If the two counters are equal, we will gotologin()!

Let me explain: case 1 can be captured at any time, because when starting a new activity, our startcounter will always be larger than pausecounter 1 This is true because we will always have an additional activity started but not suspended

In addition, case 3 is easy to capture because once you leave our application, for example, using the home button, we will increase pausecounter and the two counters will become equal After the application recovers, onresume will decide gotologin()

Case 2 is a bit tricky, but it's also very simple The trick is to override the finish () function and decrement startcounter once and pausecounter twice Remember that onpause is called when the activity is completed and our counters are equal Now, by decrementing the startcounter once and pausecounter twice, we finally return to the counter value of the previous activity, and when the current activity resumes, the startcounter will maintain a larger pausecounter 1

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>