Up navigation using item of a navigation drawer menu - android

In my android app I have two activities, one is the LoginInActivity (i.e. MainActivity), and the other is the HomeActivity which is a child of the first activity and is implemented with a Navigation drawer. So in my manifest file I have:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:label="#string/title_activity_home"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Obvioulsy in my MainActivity I create an Intent and start the HomeActivity with startActivity() method.
Now I would like to use an item of the Navigation Drawer as if it were a Up Button, which means that I can navigate back into the hierarchy and log out.
Furthermore, I want to add more children to HomeActivity later, which is way I did not use startActivityForResult() method in MainActivity and I didn't call finish() in HomeActivity.
I'm wondering if it's possible to implement this thing. If so, how? Otherwise it is more advisable to let the HomeActivity (with Navigation drawer) become the Main, and that the LogIn is a drawer item (e.g. calls an LoginActivity with startActivityForResult())?

You can navigate back by calling onBackPressed() in your click event.

Related

Open recent activity on app icon press , after pressing the home key of device

My Launcher class name is "SplashScreen". I have two Activities "MainActivity" and "MenuList". When user opens the app, SplashScreen Activity is started. Splash Screen Activity validates and starts MainActivity. User opens MenuList Activity by clicking on button in MainActivity Screen and clicks on home button in MenuList Activity. When user opens the app it should directly open MenuList Activity. I have made changes as mentioned in below link. It is working fine when user press home button in MainActivity. It is not working when user press home button in MenuList Activity. Please help me.
https://stackoverflow.com/a/20815679/1517280
ScreenFlow:
SplashScreen -> MainActivity -> MenuList
Manifest code:
<activity
android:name=".SplashScreenActivity"
android:clearTaskOnLaunch="true"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize|stateHidden"
android:screenOrientation="portrait" />
<activity
android:name=".MenuList"
android:windowSoftInputMode="adjustResize|stateHidden"
android:screenOrientation="portrait" />
Remove android:clearTaskOnLaunch="true" from SplashActivity's element.
In fact, this doesn't work for your MainActivity either - since this flag is set, every time you get back to your application, the task's stack is being cleared and SplashActivity is being launched.

Android back button not working as intended after background

I have an activity A from which I start activity B.
If I press the back button it will go back to activity A, however if I press the home button or change to another app and come back to my app the back button closes the app instead, so I can no longer get back to activity A without restarting the app.
I use no flags when creating the intent.
Java:
Intent i = new Intent(this, InCallActivity.class);
startActivity(i);
XML:
<activity
android:name=".MainScreenActivity"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MANAGER" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity android:name=".InCallActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance"/>
Please try removing the attribute android:launchMode="singleInstance" in your activities, and check if it works.
Follow these links for more information: Activity Syntax and Launch Mode

Closing background Activity

when i'm on the MainActivity in my app then press the setting menu in the action bar a new fragment is opened..there are two problems here
1- when i'm on the setting fragment then i press the multi-tasking button i found two windows for my app not one..the MainActivity and the settings
2- if i press the setting window when i'm still on multi-tasking window then press the back button it should take me to MainActivity instead it closes the app.
So now what i want to do is
1- close the MainActivity from the background once i open settings
2- if i press multi-tasking button and choose settings window in my app then press back it takes me to the MainActivity not close the whole app.
here is how i defined my activities in manifest file
<activity android:name=".MainActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Settings"
android:label="#string/settings_title"
android:excludeFromRecents="true">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.bee.MainActivity"/>
</activity>
<receiver android:name=".Notifications"/>
</application>
hope the image helps visualizing it..thanks alot
Remove the android:launchMode="singleInstance" attribute.

How to make an activity above the MainActivity.java?

My current MainActivity is a navigation drawer.
I want to make another activity on top of the navigation drawer.
Lets make that activity StartActivity.
On StartActivity there is a start button.
What i want to do is make the StartActivity opens up first when the app runs.
And when the user presses the START button on the StartActivity, it will direct him/her to the navigation drawer.
is this possible?
Create the StartActivity.
somewhere call "startActivity(intentforMainactivity);
go to the manifest and move
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
from the
<activity
android:name="sehtestapp.MainActivity"
into
<activity
android:name="sehtestapp.StartActivity"
Your Application will then start the StartActivity first
1) Create another Activity, that you'll call StartActivity
2) Set up a layout that you'll use in your StartActivity with a
button in it.
3)Create the onClickListener to launch a new Intent when clicked.
Make it launch the MainActivity.
4) Change your AndroidManifest as follow :
<activity
android:name="XXX.StartActivity"
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="XXX.MainActivity"
android:parentActivityName="XXX.StartActivity" />
You can try changing the Launcher activity and send intent having a boolean bundled in it when START button is pressed, in MainActivity fetch the intent, read that boolean value and open the drawer programmatically.

How open not main activity from Notification

In my app I have a Activity that is opened from a Notification, but is not declared has main activity or launcher activity·
Also, this activity is declared has "singleTop".
Works fine for most users, but some are a little problem.
In some occassions when click the Notification the Activity is opened and, when click back, the app Main Activity is shown.
How to don't show the main activity when click back?
Also, if the users are in this activity and click home, and later it clicks into the app icon in order to open the Main activity, the previously opened activity, that is not main activity is opened. This can change?
Thanks.
-- The structure of the manifest is:
<activity android:name=".MainActivity" android:label="#string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowAlarmsActivity" android:launchMode="singleTop" android:exported="true" android:theme="#style/AppThemeWithoutActionBar" />
you may do this my either overriding on back press and starting the parent activity using this code
or if you are using action bar you may use parent activity tag
Intent intent = new Intent(getApplicationContext(), ParentActivity.class);
startActivity(intent);
also if you want to distinguish that if the user is coming from notification or from app launch you may use intent to pass some key value and cater it
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="parentActivity" />

Categories

Resources