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
I've been tweaking a lot with android design and capability of Android to use the maximum potential of an Android framework, I have come across transition, and my question is how to define Activity's Destroyed's animation ?. Say I am starting an activity using intent like so :
Intent intent_info = new Intent(ComponentsPage.this, SecondActivity.class);
startActivity(intent_info);
overridePendingTransition(R.anim.slide_up, R.anim.no_change);
That snippet basically opens up SecondActivity with Slide Up transition. Now i am in second activity and second activity say doesn't have any button but i want whenever Second Activity is closing (Destroyed) it close with slide down animation.
I've tried adding
overridePendingTransition(R.anim.no_change,R.anim.slide_down);
Inside onDestroy() and onStop(), but still no luck, i guess the activity is already closed when those methods are called.
When you try to finish the second activity try to override the finish method inside your SecondActivity as follows:
#Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.no_change, R.anim.slide_down);
}
I have an application where there are: a splash screen, a main activity (started from the splash screen) where there's a navigationView that attach and detach some fragment.
In one fragment i need to download some information with an AsyncTask. The problem is that the AsyncTask is called in the splash screen (the splash screen survive for 3 seconds) and the download can takes more or less than 3 seconds. In the onPostExecute() in AsyncTask i want to call a function loadFeeback() (that is in the fragment class) only if the fragment is already loaded, if not i set a static variable downloaded to true.
public void onPostExecute(Feedback[] feedbacks){
Dati.feedbacks = feedbacks;
if(mainActivity != null) {
FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
FeedbackFragment feedbackFragment = ((FeedbackFragment) (fragmentManager.findFragmentById(R.id.nav_feedback)));
Dati.feedbackDownloaded = true;
if (feedbackFragment != null)
feedbackFragment.loadFeeback();
}
}
The problem is that i can't get the mainActivity reference and i can't pass the context to the AsyncTask because it is called from the splash screen.
Can anyone help me?
The Asynctask is tied to the splash activity,because it created it.
Maybe you can use a Headless Fragment( fragment without layout):
Create a fragment on precise in the onCreate function :
setRetainInstance(true)
In your splash activity create the fragment and save him with a tag :
HeadlessFragment headlessFragment = new HeadlessFragment()
getSupportFragmentMangager().beginTransaction().add(headlessFragment, "fragmenttag").commit();
Launch your asynctask in the fragment
And when your main activy is launched retrieved the fragment :
HeadlessFragment headlessFragment = getSupportFragmentMangager().findFragmentByTag("fragmenttag");
So the fragment will be attached to the main activity, and you should call whatever you need
Hope this helps.
Sorry for my poor english.
If I'm bringing Android activities from the stack to the front, how do I refresh them? So to run onCreate again etc.
My code below, in conjunction with setting activities in the Android manifest to android:launchMode="singleTask" allows me to initiate an activity if that activity is not already active within the stack, if it is active within the stack it is then brought to the front.
How do I then, if the activity is brought to the front refresh it so that onCreate is ran again etc.
Intent intent = new Intent(myActivity.this,
myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
I think FLAG_ACTIVITY_CLEAR_TOP will resolved your problem:
Intent intent = new Intent(myActivity.this, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I don't think there is an explicit way to refresh onCreate, perhaps you may want to add the code you want reloaded into onResume.
This workaround may work if you want to keep your code in onCreate.
finish();
startActivity(getIntent());
If all you want to do is refresh the content, you should override the onResume method, and add in the code to perform the refresh in this method. To do this, use the following code within the activity that you want to perform the refresh, (ie, not the same activity that you are calling startActivity() from):
#Override
protected void onResume(){
super.onResume();
//add your code to refresh the content
}
Tip: If you are using Android Studio, press Alt+Insert (while you have the Java file open), then click Override Methods, find onResume, and it should provide you with a basic template for the method.
The diagram I added shows the order that the methods are run (this is known as the Activity Lifecycle). onCreate() is run whenever an Activity is first created, followed by onStart(), followed by onResume(). As you can see, when a user returns to an Activity, onCreate() is not run again. Instead, onResume() is the first method that is called. Therefore, by putting your code into the onResume() method, it will be run when the user returns to the activity. (Unlike onCreate(), which will not be run again).
Extra info: Since you will be initially setting the data in onCreate() and then refreshing it within onResume(), you might want to consider moving all of your code used to initially set the data to onResume() as well. This will prevent redundancy.
Edit: Based on your following comment, I can give the following solution:
I'm wanting to properly refresh the page, e.g. if there is a variable count initialised at 0. And though running the activity it's has became equal to 300. When the activity is called (intent) then refreshed, count will once again be equal to it's initial value. Do you know how to do this?
Without your current activity's code, there is not much to work with, but here is some pseudo-code as to how I would accomplish your problem:
public class MyActivity extends Activity {
TextView numberTextView;
int numberToDisplay;
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(myContent);
numberTextView = (TextView) findViewById(R.id.numberTextView);
numberTextView.setText(numberToDisplay+"")//converts the integer to a string
}
#Override
protected void onResume(){
super.onResume();
numberToDisplay = 0;
numberTextView.setText(numberToDisplay+"");
}
}
I've read through several posts about using this but must be missing something as it's not working for me. My activity A has launchmode="singleTop" in the manifest. It starts activity B, with launchmode="singleInstance". Activity B opens a browser and receives an intent back, which is why it's singleInstance. I'm trying to override the back button so that the user is sent back to activity A, and can then press Back to leave the activity, rather than back to activity B again.
// activity B
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) onBackPressed();
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
startActivity(new Intent(this, UI.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
return;
}
After returning from the browser, the stack is...
A,B,Browser,B
I expect this code to change the stack to...
A
... so that pressing back once more takes the user back to the Home Screen.
Instead, it seems to change the stack to...
A,B,Browser,B,A
...as though those flags aren't there.
I tried calling finish() in activity B after startActivity, but then the back button takes me back to the browser again!
What am I missing?
I have started Activity A->B->C->D.
When the back button is pressed on Activity D I want to go to Activity A. Since A is my starting point and therefore already on the stack all the activities in top of A is cleared and you can't go back to any other Activity from A.
This actually works in my code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent a = new Intent(this,A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
return true;
}
return super.onKeyDown(keyCode, event);
}
#bitestar has the correct solution, but there is one more step:
It was hidden away in the docs, however you must change the launchMode of the Activity to anything other than standard. Otherwise it will be destroyed and recreated instead of being reset to the top.
For this, I use FLAG_ACTIVITY_CLEAR_TOP flag for starting Intent(without FLAG_ACTIVITY_NEW_TASK)
and launchMode = "singleTask" in manifest for launched activity.
Seems like it works as I need - activity does not restart and all other activities are closed.
Though this question already has sufficient answers, I thought somebody would want to know why this flag works in this peculiar manner, This is what I found in Android documentation
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
So, Either,
1. Change the launchMode of the Activity A to something else from standard (ie. singleTask or something). Then your flag FLAG_ACTIVITY_CLEAR_TOP will not restart your Activity A.
or,
2. Use Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP as your flag. Then it will work the way you desire.
I use three flags to resolve the problem:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
Add android:noHistory="true" in manifest file .
<manifest >
<activity
android:name="UI"
android:noHistory="true"/>
</manifest>
i called activity_name.this.finish() after starting new intent and it worked for me.
I tried "FLAG_ACTIVITY_CLEAR_TOP" and "FLAG_ACTIVITY_NEW_TASK"
But it won't work for me... I am not suggesting this solution for use but if setting flag won't work for you than you can try this..But still i recommend don't use it
I know that there's already an accepted answer, but I don't see how it works for the OP because I don't think FLAG_ACTIVITY_CLEAR_TOP is meaningful in his particular case. That flag is relevant only with activities in the same task. Based on his description, each activity is in its own task: A, B, and the browser.
Something that is maybe throwing him off is that A is singleTop, when it should be singleTask. If A is singleTop, and B starts A, then a new A will be created because A is not in B's task. From the documentation for singleTop:
"If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance..."
Since B starts A, the current task is B's task, which is for a singleInstance and therefore cannot include A. Use singleTask to achieve the desired result there because then the system will find the task that has A and bring that task to the foreground.
Lastly, after B has started A, and the user presses back from A, the OP does not want to see either B or the browser. To achieve this, calling finish() in B is correct; again, FLAG_ACTIVITY_CLEAR_TOP won't remove the other activities in A's task because his other activities are all in different tasks. The piece that he was missing, though is that B should also use FLAG_ACTIVITY_NO_HISTORY when firing the intent for the browser. Note: if the browser is already running prior to even starting the OP's application, then of course you will see the browser when pressing back from A. So to really test this, be sure to back out of the browser before starting the application.
FLAG_ACTIVITY_NEW_TASK is the problem here which initiates a new task .Just remove it & you are done.
Well I recommend you to read what every Flag does before working with them
Read this & Intent Flags here
Initially, I also had a problem getting FLAG_ACTIVITY_CLEAR_TOP to work. Eventually, I got it to work by using the value of it (0x04000000). So looks like there's an Eclipse/compiler issue. But unfortunately, the surviving activity is restarted, which is not what I want. So looks like there's no easy solution.