Check if current activity is required activity (same parent) - android

I have DrawerParent class which is basically a drawer. And, say, two activities inherited from DrawerParent. What I want is to not re-open same activity if it is already running. For such thing I need to somehow check whether this activity is running or not. It works like this in drawer:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_about) {
startActivity(new Intent(this, AboutActivity.class));
finish();
} else ...
And I don't know how to check it. Thank you.
== Edit ==
Making group in layout and adding selectable="single" not works.

If you are running that code on the activity, you can compare using:
if (!(this instanceof ActivityToBeOpened)) {
startActivity(new Intent(this, ActivityToBeOpened.class));
finish();
}
You can get more info about instanceof here.
Hope it helps!

Related

Start Activity in same intent

i want to know how to start all my activities in same intent. My app have a drawer activity and i want when i select any item, it will start an activity in the same intent, how can i do that ?
Main Acitivy
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
return true;
I checked Google Drive and I'm guessing you're referring to the behavior that when you click on something only a part of the screen updates. You can easily implement this by using Fragments. I cannot explain it to you in detail but this is a developer.google link that will help you get started. https://developer.android.com/training/basics/fragments/index.html

How to open an activity on click of a DrawerLayout navigationView item in android?

What function should i use to open an activity when a DrawerLayout navigationView item is clicked, and do i need to make object for that activity?
Here is my code so far:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
// Handle the camera action
} else if (id == R.id.nav_cv) {
} else if (id == R.id.nav_subscription) {
} else if (id == R.id.nav_logout) {
}
You can open activity like this.
Intent I=new Intent(MainActivity.this,actvitytoopen.class);
StartActivity(I);
You better use fragments instead of activities and change them using FragmentManager. You can find plenty of tutorials just by searching on Google/Youtube.
You can start new activity by
Intent intent = new Intent(this,NextActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
for further info refer https://developer.android.com/training/basics/firstapp/starting-activity.html
I spent a lot of time trying to call a new activity by clicking on navigation drawer item . but it is virtually impossible and if it is possible in any other way I tried, then it is sure that on the called activity there will be no drawer navigation drawer then. Fragment is a part of similar activity so calling fragment is the correct way. If you are calling fragment, navigation drawer will still be there on the side pane and you would be able to select between different items of the drawer.

Issue while moving from current activity to previous activity using getSupportActionBar().setDisplayHomeAsUpEnabled(true)

I have few activities and I want to move current activity to previous, like A->B->C-D, here i want to go to D->c->B->A, I have added respective parent activity name in manifest for all activities but when i press arrow in activity D its goes to activity A directly, if i backpress then it is coming to C activity,
How to to it properly?
You may override your onOptionsItemSelected-method to achieve the same behaviour in both cases...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
onBackPressed();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
This is a regular proceed of your back button. You can read more about it at the official website.
You can set flags to your intent, if you want to get other behavior.
Link

How to use a navigation drawer in Android Studio?

First time here.
I need to use Navigation drawer for my app. But im working with Activities.
The question is, how can i select an item and redirect to an activity?
Thank you
If you used the Navigation Drawer Template from Android Studio, you will have a main activity with a method onNavigationItemSelected. Implement the method to check the id of the menu item passed to it and decide which Activity to start based on the menu id. For example:
public boolean onNavigationItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.nav_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}else if (id == R.id.nav_faq) {
startActivity(new Intent(this, FAQActivity.class));
return true;
}
}

Pressing back button calls onCreate (Android)

Activity A starts Activity B. In Activity B, I have this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpTo(this, new Intent(this,
ArticleListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
When I press that home button, it takes me to Activity A as it should. However, onCreate is being called again. I do not want this behavior.
I'm guessing its because this implementation uses new Intent to previous item in the navigation stack. This is just code I got from Eclipse when creating the double pane project though. I looked around on stack overflow, and though it seems that using an Intent to go back is causing this behavior, I do not understand why Google would provide this in a default template.
How should I make this call differently so that onCreate is not called again when going back to Activity A?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
just finish current activity it will take you on previous activity
if your previous activity is activity A then just use finish(); method instead of creating object of intent
As pointed out by #Rajesh you need to call onBackPressed().
Your code detects the home button press and creates a new ArticleListActivity class.However, you don't want to create a new class you only want to go back to your already created class/activity. so use onBackPressed() instead.
You can try to set the launch mode in the manifest, because the parent activity can be popped off the stack.
android:launchMode="singleTop"
In this way, the onCreate() is not called again.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

Categories

Resources