Remove multiple Activities to go Back to Dashboard from Options menu - android

I have a Optionsmenu in my Android App, in which is a Button to Go Back to the Apps Dashboad and remove all Activities laying upon that, also removing the History. How is this possible?
thx
Got the Answer:
That's what does the Trick:
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.dashboard:
Intent myIntent = new Intent(this, DashBoard.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

That's what does the Trick:
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Related

Action bar on Action back (Navigation Up) to custom activity?

I have many activities in my application
in that I have Given this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
to Go back to Home Page from other activities
like Below
If I press those three buttons all menus are working
this is my Option menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.abc) {
Intent mypIntent = new Intent(this, abc.class);
startActivity(mypIntent);
return true;
}
else if (id == R.id.web) {
Intent webIntent = new Intent(this, Web.class);
startActivity(webIntent);
return true;
}
else if (id == R.id.about) {
Intent aboutIntent = new Intent(this, About.class);
startActivity(aboutIntent);
return true;
}
.
.
.
..
return super.onOptionsItemSelected(item);
}
Here There is no menu named id == R.id.login or id == R.id.home but its going to login few days back its gone to home activity
but If I press Back.. action back is redirect to Login page Inst-ed of Home
I have added a Login page for my application using shared preferences.. and it is now launcher activity..
Here In my Login activity on if once user is sign in it should it should redirect to Home activity on every time..
and its working fine..
But on action bar when I press arrow button it is redirecting to empty login page..
if I press cancel entire app is working .. my credentials are safe except this action bar..
Update
I have Given Intent also if Login credentials success redirect to Home activity on app start up it will check every time
every thing is as for fine except action back
how to fix this...
Alright make sure u do declare activities as below -
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
Now in every activity add below code block-
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Update
add a New class file to check login or not else use home as default.. and replace your new class as launcher in Manifest
Just override this method.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
{
Intent intent = new Intent(mContext, Activity.class);/*your activity name*/
startActivity(intent);
}
default:
return super.onOptionsItemSelected(item);
}
}

How to prevent .setDisplayHomeAsUpEnabled from re-creating the parent activity?

When I press the "back/up" button in the toolbar to go up one level, the parent activity is re-creating itself (re-loading data, etc.) when I'd just like to finish the current activity.
Right now my activity is automatically creating the Toolbar (support Toolbar), so I'm not sure how to select it to call NavUtils.navigateUpFromSameTask(this); when it's clicked.
Is there a simple flag I can set it to tell it to just finish instead of re-creating the parent activity?
I use this trick and works, returns back to parent without re-creating the activity. The commented code recreates the parent. so i used finish()
#Override
public Intent getSupportParentActivityIntent() {
/*String from = getIntent().getExtras().getString("from");
Intent newIntent = null;
if(from.equals("MAIN")){
newIntent = new Intent(this, MainActivity.class);
}else if(from.equals("FAV")){
newIntent = new Intent(this, FavoriteActivity.class);
}
return newIntent;*/
finish();
return null;
}
in manifest delete android:parentActivityName="YourParentActivity" if you want to disable it completely
update
override onOptionItemSelected() if you want to change its behavior
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
}

Cannot be cast the activity

I've got a little problem casting an activity and when I click the item in my menu to open the activity the app crashes and the logcat says that it can be cast the activity. The problem is in this line:
_Sapp = (SecondActivityApp)getApplicationContext();
I think it's the getApplicationContext but I'm not sure. How can I resolve it?
If you are trying to start a new Activity from the menu, you'll have to use an Intent just like you would, if you wanted to start an activity from any other place.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_testdone:
Intent intent = new Intent(getApplicationContext(), SecondActivityApp.class);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

How can I get Android ActionBar to goto Home when I click upper left?

I am seeing the up caret but nothing happens when I click it. Where do i tell it to goto home and set which activity is Home?
You need the line:
getActionBar().setDisplayHomeAsUpEnabled(true);
put that in onCreate();
That will make it clickable. The you will need to handle the click event. This is done by overriding onOptionsItemSelected()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
If you want to use NavUtils, (which I have seen used by the ADT plugin when it makes Activities) you can replace change the implementation like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't forget to
import android.support.v4.app.NavUtils;
Also, you can add this to your Activity node:
<activity
android:name=".SomeActivity"
android:label="#string/activity_label" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.MainActivity" />
</activity>
NavUtils is usually the better approach. For more information, see the official Android Guidelines.
In your onCreate you have:
getActionBar().setDisplayHomeAsUpEnabled(true);
And then use:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Updated to address comments below, which correctly point out my original answer was not correct for all cases. The "home/up" button have are supposed to take you the Home Activity or the Parent Activity, respectively. From the ActionBar documents --
By default, your application icon appears in the action bar on the
left side. If you'd like, you can enable the icon to behave as an
action item. In response to user action on the icon, your application
should do one of two things:
Go to the application "home" activity, or
Navigate "up" the application's structural hierarchy
Implementing "Home" is straightforward. Launch an intent to the home activity (usually your main launch activity) of your app, clearing the backstack to the existing instance if one exists--
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
"up" is similar, except you should use
getActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate to get the icon to show as "up" instead of "home", and launch an instance of the parent activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In both case, for Activities that can be launched from outside your application, you should add FLAG_ACTIVITY_NEW_TASK to launch into a new task instead of the callers task.
"Up" can get more complicated if your Activity can be launched from another app, and want to build a back stack for your application to get back to the root. See for this guide for more info.
My original answer, shown below, is considered bad form in the guide because it it treats the home/up button as 'back'. It will take you to the activity that invoked this activity, which is not necessarily the home or parent. It works well as 'up' for apps that have strict tree hierarchy of activities because an given activity was always launched by its parent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How to call Activity from a menu item in Android?

I'm attempting to call startActivity(myIntent) from the click of a menu button but my application crashes at that point.
The same startActivity call works fine from a regular button click, so, I assume the menu button is missing information about the context? Or maybe I'm totally off the mark here.
So... what's the correct way to have a menu item take me to a specific Activity?
I've revised my code based on the initial set of advice. Still crashing in the same place. The debugger doesn't enter the exception clause, the app just dies.
[EDITED WITH CODE SNIPPET]
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
try{
switch (item.getItemId()) {
case R.id.menuItemLang:
startActivity(new Intent("com.my.project.SETTINGS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}catch(Exception e){
log(e);
}
}
First option
You have to override onOptionsItemSelected method in your Activity, which is called when user clicks on the item in Options menu. In the method you can check what item has been clicked.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
There is also onContextItemSelected method which works similary, but for Context menu (I'm not sure, what menu you mean).
More information at http://developer.android.com/guide/topics/ui/menus.html
EDIT:
Second option
I think the first option is easier, but from your code I see, that you want to start activity as an action (because of String parameter in Intent constructor). To do this, you need to specify an action in your AndroidManifest.xml. So, if I would start activity ActivityForItemOne (from previous example) the <application> element in AndroidManifest.xml would look like this:
<application ...>
...
<activity android:label="Activity For First Item" android:name=".ActivityForItemOne">
<intent-filter>
<action android:name="my.app.ITEMONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
And the Intent will be:
Intent intent = new Intent("my.app.ITEMONE");
The my.app. is package of your application. It's not necessary to use your application package, but it's recommended for uniqueness of actions.
More information at:
Class Intent - Action and Category constants
Action element
Intents and Intent Filters
Hope this solve your problem.
More optimice:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
return true;
case R.id.item2:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
if There have 2 Class
1 MainActivity
2 Welcome
then you need to go from
welcom>MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.logout:
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
break;
case R.id.settings:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}

Categories

Resources