1

I have searched a lot for this ,but nothing gave a solution. I will explain you the scenario: my application is with login page -> User verification page-> Home page (Home page with multiple menus and a footer -Horizontal scroll view). Application login only happens for the first time or when user logout.On successful login ,user is navigated to the authentication page and login activity is finished .My code is

    Intent loginIntent = new Intent(Login.this,UserAu.class);
    loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(loginIntent);
    finish();

On successful authentication, user is navigated to application home page and again the authentication activity is finished.

    Intent proceedIntent = new Intent(UserAu.this,Home.class);
    proceedIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    proceedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);             
    startActivity(proceedIntent);
    finish();

Now the user is in Home page and he clicked Menu 1 resulting in Activity A then to Activity B from A. These activities are not finished .Logout button is in footer (which is a class extending a Linear layout )and when user logout ,the current activity is finished and navigated to login screen using

    Intent loginscreen=new Intent(context,Login.class);
    loginscreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    loginscreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(loginscreen);
    ((Activity) context).finish();

My issue occurs when user press back button from Login page ,its navigating to Activity A . I tried this on Login class

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

But now when the user takes the application again its navigating to Home page.Again if user press back button, Application is closed and only loads the Login page afterwards. I searched for a solution but no results. Please give me a solution.Thanks in advance.

5 Answers5

0

try this also

<activity
            android:name="AccountSetting"
            android:launchMode="singleTop"
            android:screenOrientation="portrait" >
        </activity>

do this entry in your manifest file

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • Is it for the Launch activity? or for all activities? – Feby Elizabeth Dec 04 '13 at 06:03
  • I tried this : added this in Login activity and home, retained the same code. bt, application launches only for 2nd click every time. – Feby Elizabeth Dec 04 '13 at 06:30
  • ie, in manifest for Login , added android:launchMode="singleTop" and also did the same for home I made only this change (for login and home in manifest) My java code remains the same. Its working fine . bt issue is application is launched only for the 2nd time click on the application icon. Every time you need to click twice to launch the app. get me?? – Feby Elizabeth Dec 04 '13 at 06:37
  • ohk and also add this falg in intent intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); and till now when u back press then activity is not finish – Bhanu Sharma Dec 04 '13 at 06:39
  • Intent intent = new Intent(this, Secondactivity.class); startActivity(intent); finish(); – Bhanu Sharma Dec 04 '13 at 06:49
  • frim this it will never be see when u click back button – Bhanu Sharma Dec 04 '13 at 06:50
  • U mean the activities from Home?? How can be it possble for a situation like view the selected item in detail from a list .ie, ACtivity 1 is my list and activity 2 is my Details page . so when the user press back button from activity 2,we need to show the list again rt?? am i wrong?? – Feby Elizabeth Dec 04 '13 at 07:14
  • no no no only on login button and splash both screen never come when we press back on that screen i suggest this on other u just Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK. and that main feast entry that all – Bhanu Sharma Dec 04 '13 at 07:24
0

This issue is occurring because you are finishing login activity and authentication activity and then you are using Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK. These flags don't seem to work with finished activities

I faced the same issue for my app, what i did was not finish these activities and then used above written flags with intent. Now, if you don't finish those activities then user will navigate back from Activity A he will again go to login or authentication activity. For this I interpreted backPressed and left it empty in my home activity.

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    //do nothing
}

User will navigate back to device using home button.

DroidDev
  • 1,527
  • 4
  • 20
  • 42
  • that means you are disabling back button, rt? but i must use the back button..Its a requirement – Feby Elizabeth Dec 04 '13 at 06:18
  • @FebyElizabeth :well then i should tell you that these flags will only work(in my experience) if the previous activities in the stack were not closed. Now, if you don't want to disable the back button in your home activity, then can you please explain how do you want to use it, do you want to go back to login activity? – DroidDev Dec 04 '13 at 06:26
  • I finished my login activity & verification page ,then navigated to home, so when u press back button its back to device home . On launching the app again, i am checking a flag in whether the user logout or not, if not show the home again. otherwise the login. – Feby Elizabeth Dec 04 '13 at 07:02
  • @FebyElizabeth well then you can try http://stackoverflow.com/a/19806422/2389078 , but let me tell you that __this is not good way of programming(you can see from comments)__. But using this way you will be able to finish you login and authentication activities on back press of you home activity. On relaunch of app, you are already checking if user logged out or not. So, that won't be a problem that which activity to launch. – DroidDev Dec 04 '13 at 07:09
0

I think..when you click on back at home page..then application should be close..if this is what you want then try this one

 public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    //Handle the back button
    if(keyCode == KeyEvent.KEYCODE_BACK) 
    {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
    }
    else
    {
        return super.onKeyDown(keyCode, event);
    }
}
Yash
  • 41
  • 1
  • 1
  • 7
0

Try this

Within login screen, We are bringing the home screen ogf OS to tpo , which is equivalent of what you do by movetasktoback

  @Override
 public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

Try this in your AndroidManifest.xml. For the activity ,which should not be launched again.

<activity
        ....
        android:excludeFromRecents="true"
        android:exported="true"
        android:launchMode="singleTop"
</activity>
Swapnil Kale
  • 2,620
  • 2
  • 23
  • 21