I am building an application for android and I am not able to make that when I click on an item inside my Navigation drawer it opens a new activity pre established by me. can you help me?
I already tried some code using intents but when I click on the item it ends up closing the application.
case R.id.homepage: {
homepage();
break;
}
case R.id.pdefault: {
testdefault();
break;
}
private void homepage(){
startActivity(new Intent(getBaseContext(),MainActivity.class));
}
private void testdefault(){
startActivity(new Intent(getBaseContext(),testdefault.class));
}
The first case worked normally because I created a method for
start the activity, but I did the same for the second and it did not work.
The expected result is just a simple screen swap.
How did you create the activity? That name is not possible.
I recommend you to use right click - new - activity - empty activity. That will create the structure needed to recognize it as an activity.
Take into account that if you create it manually you will have to override from AppCompatActivity and declare the activity in the manifest as:
<activity android:name=".SomeActivity" />
It is not just creating a class. Make sure not to use underscores for activity names, keep the format of MainActivity
Related
I have 5 different activities in my app, i enable the user to navigate between those activities by clicking buttons. The main activity has a RecyclerView in it (used to make a feed). the other activities are used for search, profile, notifications, etc. The user opens the app, the main activity is displayed. The user can scroll through the feed (which is the RecyclerView) in the main activity, now the user decides to go to other activities, search, profile, etc. Now, when the user clicks on the button that navigates to the main activity, I want to open back the main activity and display the recycler view at the same scrolling position and with the same items as was when the user just left it off.
Note: I'm not talking here about pressing the back button or using any goBack functions. I want to enable the user to use the main activity and visit all other activities and after all of that he will be able to come back to the main activity and find the feed exactly as he left it off.
In every activity I have five navigation buttons that allowing the user to go from every activity to every activity: click on profileBtn will open the profile activity, etc.
There is a way in which i can back up the state of the recycler view in the main activity before the user wants to go to other activities and restore it when the user clicks on the homeBtn?
Code:
HomeActivity.java:
public void profileBtn_OnClick(android.view.View view)
{
Intent intent = new Intent(getBaseContext(), ProfileActivity.class);
startActivity(intent);
}
ProfileActivity.java, SerachActivity.java, NotificationsActivity.java:
public void homeBtn_OnClick(android.view.View view)
{
/* HERE I WANT TO OPEN BACK THE HOME ACTIVITY AND DISPLAY THE FEED
EXACTLY AS IT WAS WHEN THE USER LEFT IT, HOW DO I DO THAT? */
Intent intent = new Intent(getBaseContext(), HomeActivity.class);
startActivity(intent);
}
You can use the launchMode="singleTask".
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to the existing instance through
a call to its onNewIntent() method, rather than creating a new one.
You can get more information about launchMode here: https://developer.android.com/guide/topics/manifest/activity-element
android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]
<activity
android:name=".ui.HomeActivity"
android:launchMode="singleTask" />
Please do NOT use the special launch mode singleTask for this purpose. It causes more problems than it solves. These launch modes have complex side-effects that are not obvious and will cause you more problems later.
Your problem is simple to solve using standard Android behaviour. Change your method to look like this:
public void homeBtn_OnClick(android.view.View view)
{
Intent intent = new Intent(getBaseContext(), HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
The flag CLEAR_TOP tells Android to remove all activities from the task that are on top of the target Activity (in this case the target Activity is HomeActivity.
The flag SINGLE_TOP tells Android not to recreate HomeActivity, but to reuse the existing instance of it.
I've a little problem. I've two activities (GalleryActivity and GalleryVideoActivity) and after swiping between them, i would to come back to the first activity (GalleryActivity) pressing just one time on back button because i've to press back button as many times as i swipped. Is it possible? Thanks in advance to everyone!
So if i understand your issue is that you are moving between activities and everytime you move they create new instances so as a solution to suggest is as following ,
in your manifest file , in activity section add launchMode="SingleInstance" , as this would create only one instance of that activity .
This is an example
<activity
android:name=".ui.apppassword.PasswordRestoreActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance"/>
In GalleryVideoActivity override onBackpress method and navigate back to the GalleryActivity by putting simple intent.
Here is what you need to add to GalleryVideoActivity in order to implement onBackPressed as Mike stated:
#Override
public void onBackPressed() {
Intent toGalleryActivity = new Intent(this, GalleryActivity.class);
startActivity(toGalleryActivity);
}
This starts GalleryActivity everytime the back button is pressed in GalleryVideoActivity.
I have an Application min SDK 21, target 25. Using : android.app.DialogFragment and android.support.v7.app.AppCompatActivity.
The initial Activity is a Launcher it can launch 1 of 3 Activities and 1 DialogFragment. The DialogFragment can in turn launch an Activity (which is one of the 3 Activities). The problem that I have is only with regard to the DialogFragment being loaded and that Fragment then loading an Application and then returning to the Launcher.
When the Fragment returns immediately to the Launcher (without loading the Activity), this works OK. I can detect that the Fragment was loaded via the launcher and onBackPress() handles it OK.
However, when the Fragment loads another Activity and then onBackPress()is used to return to the Fragment and then onBackPress()is used to return to the Launcher, I have a problem. The way this situation is currently handled is that the Fragment loads the Launcher via an Intent using the flags FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP (this appeared to be a solution to someone’s similar problem). I have tried other variations of these flags and also without these flags. I have also tried to use for this situation onBackPress()in the DialogFragment without loading the Activity using an Intent, and the Application terminates, and I need it to return to the Launcher.
In this case, using these flags, when I return to the Launcher, the Launcher displays and then clears and then redisplays (on another onBackPress(), the Application then terminates correctly). The problem is with the Launcher displaying twice. I presume that the Launcher that displays first is the original copy loaded and then it is replaced by a new copy. I have not found a way (in this scenario) in which to load the original copy of the Launcher (if that is in fact what is happening). As stated, this is a DialogFragment.
How can I solve this so that the Launcher Activity does not display twice in this situation?
Why you don't just finish the activity within the fragment to return to the launcher activity?
If you really have to use a intent, try Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
Try overriding the onBackPressed() method as follows:
#Override
public void onBackPressed() {
int count = fragmentManager.getBackStackEntryCount();
if (count == 1) {
finish();
} else {
fragmentManager.popBackStack();
}
}
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStackImmediate();
}
else {
super.onBackPressed();
}
use this in onBackPressed() method.
I want to create an intent in MainActivity that will launch CompassActivity.
Both classes share a common layout activity_main, but use different parts of it.
Here is the intent I currently have in MainActivity which is supposed to open the CompassActivity.java class file but doesn't.
public void startCompass(View v)
{
Intent intent = new Intent(this, CompassActivity.class);
startActivity(intent);
}
What I have tried:
Altering androidmainfest.xml.
Changing the intent itself.
Changing the SDK version.
All you have to do is place the correct variables under the onCreate method and it should work fine.
Do you want to open an intent or do you just want to show/hide the different views you are using?
Instantiate your Views (findViewById) in your onCreate, then you can call view.setVisibility(View.GONE or View.VISIBILE) to hide or display your different views.
Bottom line, your views exist already on the main Activity (so no Intent needed to start another Activity) - you just need to control which views are visible at a particular time.
Normally In an Fragment, implicit intent can be used to start a brower component,if there are several brower component, the Android system will show an selection dialog ,the dialog just overlay the Fragment which send the intent.
but in my situation when selection dialog is shown ,the Fragment go to the background(disappeared) ,and the home screen can be seen.
does anyone encounter this problem ,how to make the Fragment not go to the backgoround.
in onPause method it just look like below in my fragment source
#Override
public void onPause() {
mLog.printMethodLifeCycle("onPause");
super.onPause();
}
and in activity I did not overide the onPause method
AndroidManifest.xml is too long ,can you tell me which part should I post or notice