Start Activity in same intent - android

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

Related

Check if current activity is required activity (same parent)

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!

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.

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;
}
}

Back button behavior on an Activity

I have a problem in my app. I have a MainActivity with Fragments, each Fragment have a list with a recyclerView. When I click an Item of the list, the app goes to a new DetailActivity.
In the DetailActivity I have the next code line:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
When I click the back button in the ActionBar I return to the MainActivity in another fragment, not the one I clicked the item.
Thanks!
As stated by #kareem adel make sure you are not ending the previous activity. Dont use Flag_activity_clear_task or flag_activity_new_task
If that doesn't help you could just tell the back button where to go.
With getSupportActionBar().setDisplayHomeAsUpEnabled(true); set you can do this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if(id == android.R.id.home){
//Back button was hit so go somewhere.
Intent i= new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); //Clear previous Activities
startActivity(i);
finish(); //End this activity.
return true;
}
return super.onOptionsItemSelected(item);
}

Evaluating R.id.home

I was following this tutorial here - http://developer.android.com/training/implementing-navigation/ancestral.html - for implementing the Up Navigation. Sort of the same as the user pressing the Back button on the phone, but the onBackPressed() method does not fire when the 'Up' button is pressed. In the tutorial they show that you trap R.id.home in the onOptionsItemSelected() method. This web page - http://developer.android.com/reference/android/R.id.html - shows that the value of R.id.home should equal 16908332, but it doesn't in my app. In the code below if I use R.id.home it fails. If I hard-code in 16908332 it works. For me R.id.home evaluates to 21312330724. According to the page, all of the R.id values begin 1690. I hate hard-coding in a value for a built-in value, but I'm not sure what else to do. Could this cause problems down the road? Am I doing something wrong? Is this a bug?
Greg
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.home) {//16908332
Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.putExtra(CAT_ID, CatID);
upIntent.putExtra(USER_ID, UserID);
upIntent.putExtra(LIST_ID, ListID);
setResult(RESULT_OK, upIntent);
NavUtils.navigateUpTo(this, upIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
You need to compare it to the android one:
if (id == android.R.id.home){
...
}

Categories

Resources