This question already has answers here:
How to restart Activity in Android
(22 answers)
Closed 8 years ago.
in an app that I am writing, there is a part of it that allows you to change a curtain setting. the problem is, that this setting won't take effect until the activity is recreated. is there a way to tell the app to restart using the onResume() method (hopefully allowing it to save everything in the onSaveInstanceState())?
This has been posted before:
Intent intent = getIntent();
finish();
startActivity(intent);
As of API level 11, you can also just call an activity's recreate() method. Not only is this cleaner because it is less code, it avoids issues that may arise if your activity was launched by an implicit intent.
Perhaps you could restart the activity as has been demonstrated, but pass in some intent extras to send your string back when it re-starts.
Intent intent = getIntent();
intent.putExtra(STRINGTOSAVE, "Save this string");
finish();
startActivity(intent);
and in your onCreate you would of course want to retrieve the string
Intent intent = getIntent();
String STRINGTOSAVE = intent.getStringExtra(ActivityName.STRINGTOSAVE);
and then use the retrieved string to reapply the textfield and any other actions you need.
Related
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
How to use putExtra() and getExtra() for string data
(18 answers)
Closed 5 years ago.
I'm writing an application in android studio IDE which shows the models,brands ,prices and etc of the cars and I want to move from one activity to another one.can I use putExtra?if yes i wonder if anyone would like to tell me how can i use it.
You can pass data between activities in easiest way like this.
In the first activity use out extra to send the data
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("YOUR_DATA_KEY", data);
startActivity(intent);
Access that intent on next activity like this
String s = getIntent().getStringExtra("YOUR_DATA_KEY");
try this
Intent intent=new Intent(this,YourActivity.class);
intent.putExtra("key",value);
startActivity(intent);
Try the following
Intent intent =new Intent(FirstActivity.this,SecondeActivity.class);
StartActivity(intent);
This question already has answers here:
How to manage startActivityForResult on Android
(14 answers)
Closed 7 years ago.
I have a hitcount that I need to be passed to another activity I know how to receive it but I'm not sure how to format it to make sure it gets passed to the other activity. The activity with the receiver is named GameActivity. This is my receiver:
public void finish(){
Intent returnIntent = new Intent();
returnIntent.putExtra("GAME_SCORE",gameView.getHitCount());
setResult(RESULT_OK, returnIntent);
super.finish();
}
I know I have to create a getHitCount field that will pass it to the intent but I'm not sure how to do this. The relevant field is called hitCount, I've also included where it is incremented.
if (sprite.wasItTouched(event.getX(), event.getY())){
/* For now, just renew the Sprite */
sprite = new Sprite(this);
hitCount++;
}
public void getHitCount() {
}
StartActivityForResult is good as suggested by the previous answer.
For the retrieving part
Well u can get the value using getIntExtra .but it consists of a argument called as default value. for that i suggest you provide the previous game score.
Intent in=this.getIntent();
hitCount = in.getIntExtra("GAME_SCORE",default_value);
Is you use setResult you get the Integer in the method startactivityforresult, I recommend you use a preference for save you number in disk and get this data in any other part in your app
Use startActivityForResult(intent); to get intent from next activity when it finished. You can get result in onActivityResult(..,..,.) method.
I know this is probably extremely simple, but I just can not figure it out.
I'm trying to reload/recreate an activity after an action. I know I could just use:
Intent intent = getIntent();
finish();
startActivity(intent);
But in reading through answers on the site I'm told to use 'recreate()' after 11 api. Any help would be appreciated, thanks!
While using the recreate method works by doing
this.recreate()
It was only added in API level 11. If you want to include more devices you can check the API level and implement both the recreate method as well as
Intent intent = getIntent();
finish();
startActivity(intent);
You can use both by making an if statement like...
if (android.os.Build.VERSION.SDK_INT >= 11) {
//Code for recreate
recreate();
} else {
//Code for Intent
Intent intent = getIntent();
finish();
startActivity(intent);
}
this.recreate() is all it takes. Stick that code inside a method that lives in the activity you want to reload. I have a project where this is tied to a button click but you can use it however you need to.
I'm a bit confused by your question, you yourself answered the question in your answer. Call the method recreate directly...
From the documentation for recreate():
Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it.
Call recreate() from within the activity code instead of
Intent intent = getIntent();
finish();
startActivity(intent);
to restart the activity (after API 11 that is).
See this answer for a more generic recreate routine that works even for before API/SDK 11.
This question already has answers here:
What is an Intent in Android?
(14 answers)
Closed 8 years ago.
I dont know much about intent (or android) so.. Can someone please explain me what is it exactly? i have search on the internet, A LOT.
Also what does each line of this code do?
Intent intent = new Intent (this, DisplayMessageActivity.class);
intent.putExtra("a", "b");
Thanks in advance
I suggest reading Android Intents
You couldn't have search for very long, since this is very basic topic.
I suggest you read more of Android's API guides.
Line 1 = Create message that describes what to do, in this case start "DisplayMessagActivity"
Line 2 = Add content to the message
Intent intent = new Intent (this, DisplayMessageActivity.class);
For this line, its function is to create a navigation from the current activity/page to the displaymessageactivity page.
it is like from here to there.
For this intent.putExtra("a", "b"); the purpose of this is to put like a temp storage/variable to pass to the next page for retrieval. In this case, you put the value "b" in the variable "a". With this method, you can use the value on the other activity or page.
All the above are just storing of info, it is not executed yet. if you want to execute the intent do the following
startActivity(intent);
The best example to state the behavior of Intent is it behaves like a POSTMAN that delivers message to the stated address.
Whether it may be calling service ,BroadCastRecivers ,Activity they are used in number of occassion.
Intents are asynchronous messages which allow application components
to request functionality from other Android components. Intents allow
you to interact with components from the same applications as well as
with components contributed by other applications. For example, an
activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent type. Your code can
send them to the Android system defining the components you are
targeting. For example, via the startActivity() method you can define
that the intent should be used to start an activity.
An intent can contain data via a Bundle. This data can be used by the
receiving component.
Intents can be used to start Service, call Activty, call Sub Activity, transfer the data between Activity or retrieve the data from Activity
I've been reading the sample code from the dev docs on Android's site, specifically this:
http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/authenticator/AuthenticatorActivity.html
Which is the sole activity of the sample app. It refers to an intent in the onCreate method. I don't understand where this intent is coming from, or what it should contain if this is the only activity the app utilizes.
Log.i(TAG, "loading data from Intent");
final Intent intent = getIntent();
mUsername = intent.getStringExtra(PARAM_USERNAME);
mAuthtokenType = intent.getStringExtra(PARAM_AUTHTOKEN_TYPE);
mRequestNewAccount = mUsername == null;
mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRM_CREDENTIALS, false);
That's the block of code working with the intent. Why would you have an intent for the only activity in the app? Is this app called in an unusual way? The Manifest does not include an intent filter for the activity... I guess I'm just a bit lost on this whole thing! If someone could set me straight that'd be great, thanks.
Why would you have an intent for the only activity in the app?
getIntent() gets you the intent that started this activity.
Is this app called in an unusual way?
I guess this activity is called programmatically from another app or activity, since it has been passed some extra data: getStringExtra() is used to extract some data from the intent that started it. putExtra.. and getExtra.. is a way to pass data between activities when they are started.
In that specific example, the intent is sent from the addAccount method in Authenticator.java. That method is called by the OS when you click the Add Account button in the Accounts & sync settings screen and choose your account type.