If I create a new Intent of the same class every time I click a button, is the created activity the same?
Every time I click a button, I want to have a dialog show with a slider inside it and after I change it I want the state to be saved so that the next time I open up the dialog the state of the slider is the same.
My code for the button is this:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Slider_Logic.class);
v.getContext().startActivity(intent);
}
});
By "same," I assume you mean the same object instance. The answer is no. In general, when you start a new activity, it creates a new instance of that activity and pushes it onto the stack in front of the existing activity.
I say "in general" because the activity's launch mode can effect this behavior. For example, if you set launchMode to singleTop, it will create a new instance of the activity if one doesn't already exist in the target task. Please see the docs for more information.
Related
Many times in android apps, users click a button/view and start a new activity, where the new activity pops up in front of what the user was previously looking at, and loads data.
Would there be any difference from a user perspective if the data started loading (from network or disk or both) when the user clicked the button before the next activity started. And then that data was returned to the new activity in a broadcast receiver.
This is compared to starting the process in the oncreate of the activity. Assuming these network and i/o processes only take milliseconds either way, would it make a difference to the user if the methods were started in onCreate of the new activity, or started in the old activity onClick.
First way, starting I/O and changing views after I/O finishes
//first activity
public void onClick(View v){
startActivity(new Intent(this, NewActivity.class);
}
//NewActivity.class
onCreate(Bundle mBundle){
super.onCreate(mBundle);
setContentView(R.layout.mView);
mObject = networkCall(); //after network call, the view objects in this layout will reflect data from the network call
}
second way, starting the I/O in the first activity
//first activity
public void onClick(View v){
IntentService networkCall = new IntentService();
//start network call
startActivity(new Intent(this, NewActivity.class);
}
//second activity on create just sets the view and also broadcast receiver
My GUESS is that in the split second that it takes for the activity to pop up, the data from the intent service could become available. But at the same time, passing data via intent could take just as long making the benefits marginal
Insight appreciated
In my experience the onCreate() of your new activity is called almost instantly from when you call startActivity(). The new activity doesn't show up right way because it has to take time to render your layout.
You could play around with timings yourself by using the Log.d() function. Something like Log.d(TAG, "This happend at: " + System.currentTimeMillis()); at different points in your code to see when things happen. Watch the LogCat while your apps runs and you can decide for your self which way is better.
I want to keep my app as loosely coupled as possible, and most of is done with IoC
however, at some point, i need to launch different activities,and the class implementing this activity, could be any,meaning i don't want to define a specific class that starts the activity, but one needs to be set in the intent.
where is the best place to write the code to launch my activities ? is it in the same activity that starts the other activity? or have some outside logic about it?
I have an activity A
from which i need to start activity B
where do i put the logic of
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, requestingB);
It sounds like you're trying to have an activity that can be launched by some other application and you don't want the activity to necessarily know about what is launching it.
Try using an intent-filter within your activity. Then, when something needs to launch it, all it has to do is fire off an intent with the action defined in the intent-filter.
As always, Vogella has a good tutorial here: http://www.vogella.com/tutorials/AndroidIntent/article.html
As the OP mentioned in comments that he wants to start another activity on the click of a button, below is the sample code:
Button myBut = (Button) findViewById(R.id.but1);
myBut.setOnClickListener(new onClickListener()
{
#override
public void onClick(View view)
{
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
}
});
Hope this helps
In my "ArticleActivity" (where the user reads the article), I have a list of "related articles". When a user clicks one, it should reload or refresh the ArticleActivity to show the article they clicked on instead of the one they just read.
I found many many answers online and have tried roughly 492 of them... I CAN get it to start a DIFFERENT activity, but I can't get it to restart the current one.
My latest attempt:
//RELATED ARTICLE CLICK
relatedArticleClickListener = new OnClickListener()
{
public void onClick(View v) {
Log.d("MYLOG", "related article clicked " + v.getId());
Intent myIntent = new Intent(v.getContext(), ArticleActivity.class);
myIntent.putExtra("id", v.getId());
startActivity(myIntent);
finish();
}
};
Update:
Could have it to do with the ArticleActivity having this: android:launchMode="singleTask" ? I need it there, but also need to be able to reload the activity with a new article.
If I change ArticleActivity.class to MainActivity.class and leave all the rest of the code exactly the same, it DOES go to the MainActivity.
replace these lines
finish();
startActivity(myIntent);
with these
startActivity(myIntent);
finish();
After reading your update, and further understanding your question. The behavior is related to your singleTask designation. Under singleTask, if the activity already exists, it is not recreated, but instead its onNewIntent() method is invoked for you to handle the new intent. See Activity.launchmode for more info.
Do you really need singleTask? Because removing it should also cause your problem to go away. It will also allow your read articles to be in the back stack, so when your user is done with one article and presses the back key it will take them back to the first article.
Having real issue understanding how to sort my issue out.
On the Home screen I have 2 buttons
When the user clicks the first button it starts a new Activity. What I am looking for is if the user clicks back the app returns to the home screen. If the user clicks the first button again it starts a new activity.
If the user clicks the second button it returns to the activity that was last started by clicking button 1
What I am having issue with is how to save the state of the activity when the user clicks back
Also how to call that activity when the second button is pressed
Thanks for your Time
UPDATE
I have gone down part of this but still having issues. If I put some of the code I am using perhaps someone can point where I gone wrong.
Code for calling the new activity from main menu
Intent intent = new Intent(MainMenu.this, NewClass.class);
intent.putExtra("value1", value1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Within the new class I have added the following :
#Override
public void onBackPressed() {
//super.onBackPressed();
Intent intent = new Intent(RoundScoring.this, MainMenu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Toast.makeText(this, "Back Button Pressed", Toast.LENGTH_LONG).show();
}
I do not have either a onrestoreinstancestate or onresumne in this class. only a oncreate. Do I have to add something like this to bring back the instance
On the second button on the main menu I have added this
Intent intentContiune = new Intent(MainMenu.this, NewClass.class);
intentContiune.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentContiune);
Thanks
Try this:
Home Screen Left Button:
Open the new activity with an intent flag, FLAG_ACTIVITY_NEW_TASK
Activity:
Override onBackClick() on the started activity to call the home screen with an Intent instead of finishing it. Use the flag FLAG_ACTIVITY_REORDER_TO_FRONT
Save activity state overriding OnSaveInstanceState
Home Screen Right Button:
Call the activity with the flag FLAG_ACTIVITY_SINGLE_TOP
More info about flags:
http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/
One solution may involve passing bundles around that include the state of your activity. Using startActivityForResult(), you can return a bundle with the activity's state. When the user clicks your second button, pass in that bundle and have the activity set itself up with respect to the contents of the bundle. If the bundle doesn't contain the information you're looking for, then use the default values as if you were just starting it.
For more information:
Android: Capturing the return of an activity
I had a problem with an app, i.e., when I am moving to the previous window the control goes to the splash screen of my app because the previous activity is killed by the Android runtime.
How do I keep the previous activity alive, or how can I create the activity again and access it from the present activity.
You can make use of a default constructor to close and open the screen.
If you want to move between screens:
1-2-3-4
&&
4-3-2-1
Android itself will take care of it.
For example, if you want traverse in a specific way like
4-2-3-1
Android doesn't allow you, so simply create a constructor and assign present activity to it and make use of that variable to close the screen at any time.
For example, here is the activity class
public class One extends Activity
{
private One Screen;
public One()
{
Screen=this;
}
}
When you want close this activity simply use:
Screen.finish();
instead of
this.finish();
When you want invoke new activity of your liking, simply use
Screen.finish();
Intent i = new Intent(One.this, YourActivity.class);
Log.i(TAG, "calling YourActivity Screen");
startActivity(i);
If you want pass any data between the screens, you can make use of
i.putExtra("valuename", actualvalue);
and you can retrive the value using
Intent startIntent = getIntent();
String actualvalue = startIntent.getStringExtra("valuename");
When you have a Splash activity, I think the correct way is closing in and starting the main activity
Intent mainIntent =new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
Then, from your main activity start calling your subactivities.
Why has it killed your main activity? Are you calling finish()?