android remove data from getintent - java

I have an activity for handling deeplink which is a browsable activity
suppose user clicks a link on another app and my browsable activity handles that intent
and start the app, , then user minimise the app by pressing back button
class code for handling intent data
Uri link = getIntent().getData();
if user reopen app from running tasks getIntent() still have data
onDestroy method of browsable activity
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
setIntent(null);
}
setIntent(null) not working
so my question is how can i remove data from intent permanently

I am a little late on the answer here but I was dealing with a similar issue and found a solution. The reason you are still seeing data in the intent is because your app was originally started with an intent that contained data. That original intent is stored somewhere in Androids' ActivityManager and is immutable, to my understanding. When user reopens the app from "recent tasks", Android uses that original intent to recreate the application.
There is a workaround to this, however. In your apps onCreate() method, you can check to see if the Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY is set, which would allow your app to distinguish if it is being started from "recent tasks" or not, and therefore, you can avoid using the data contained in the intent.
Putting the following snippet in your onCreate() method would return true if the app is being opened from "recent tasks"
(getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0)

you have to remove data one by one key. i think you can't remove all data with one line.
if you want to remove specific key than you should use ==> getIntent().removeExtra("key"); or
getIntent().setAction("");
it will remove your data.
for more ==> Clearing intent

To remove the content it works best for me with the following lines of code.
//Clear DATA intent
intent.setData(null);
intent.replaceExtras(new Bundle());
intent.setFlags(0);
After that they can verify that the intent does not contain data

Related

How to detect whether MainActivity was launched by the app starting, or by an intent by another activity?

So I'm using Java in Android Studio and have come across a problem.
Essentially I have a value in my MainActivity, and when the user is in the EditActivity, they can edit this value. I do this by using putExtra for myValue when transferring from the MainActivity to the EditActivity, and when transferring back, the newly updated variable is named myNewValue. Essentially what I want is:
if(MainActivity is started via app launching){
textView.setText(myValue)
}else if(MainActivity is started via intent from EditActivity){
textView.setText(myNewValue)
}
In your First Screen (splash screen) . start your intent like this
intent.putextra("check_this","mainactivity");
intent.putextra(""your_value","abcd");
in your EditActivity.
intent.putextra("check_this","editactivity");
intent.putextra(""your_new_value","abcdefg");
So in your CurrentScreen.
Bundle b = getIntent.getExtra();
if(b.getString("check_this").equals("mainactivity")){
textView.setText(myValue). // this screen is opened from splash
}else{
textView.setText(myNewValue). // this is from editactivity
}

When bringing Android activities from the stack to the front, how do I refresh (reinitialize) them? So to run onCreate again etc

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+"");
}
}

Android java trouble with start new activity

In MainActivity starts another activity via startActivityForResult
Intent intent = new Intent(MainActivity.this, AutorisationForm.class);
intent.putExtra("req", 1);
startActivityForResult(intent, 0);
Method onCreate executes successful, activity displays on the screen and then app crash. Eclipse returns "source not found" error.
How I can solve this problem?
P.S. All activities declared in manifest.
P.P.S All worked successfully before I add a lot of logic in MainActivity. This code and second class hasn't changed.
Problem solved. Mistake was in onDestroy() method in MainActivity. In this method app try to save unloaded file. Theme closed.
Undo all changes before adding "a lot of logic in MainActivity."
Add changes one at a time and test in between until you discover problem.
Problem discovered!

Intent putExtra(String,String) inside of AsyncTask

I'm simply trying to carry a string onto the next activity without having to define an entire object for the task. I've seen similar solutions and gotten them to work BUT without using AsyncTask to create the intent.
protected void onPostExecute(Boolean result) {
if (loggedIn && hasPin) {
Intent intent = new Intent(UniteActivity.this,
WebViewActivity.class);
intent.putExtra(PASSED_USERNAME, passUser);
startActivity(intent);
}
if (loggedIn && !hasPin) {
Intent intent = new Intent(UniteActivity.this,
CreatePinActivity.class);
intent.putExtra(PASSED_USERNAME, passUser);
startActivity(intent);
PASSED_USERNAME is a public static constant to hold the package name, just as the putExtra() method requires. I then try to pull the value out in the next activity.
Intent extras = getIntent();
String username = extras.getStringExtra(UniteActivity.PASSED_USERNAME);
// carry username to next activity
Intent intent = new Intent(CreatePinActivity.this,WebViewActivity.class);
intent.putExtra(PASSED_USERNAME, username);
startActivity(intent);
There is never a String to pull out, the value of username is always null. I've gone through the debugger and found that the Eclipse IDE debugger shows different intent ID's between the activities, they are never consistant. Is it possible that AsyncTask is interfereing somehow because it splits into a seperate thread?
I don't know if this applies to your problem, because I can't see from your code snippet if the intermediary activity is freshly created or not.
BUT: the getIntent()-method always returns the first Intent that started the activity. If the activity remains in the background and receives a new Intent this value does not get updated automatically. You have to override onNewIntent(...) and manually call setIntent(...) for this to work (or do all your stuff directly there).
So just for the case that you do not run your posted code in the onCreate() method please check if you did not miss to fetch the real intent you are interested in.
Not sure of the exact answer for you solution.
You calling startActivity on the UI since it's in postExecute().
If all else fails you can just save that value to a sharedpreference.
The way you have handled the variable PASSED_USERNAME seems incorrect. You have used it in some palaces as simple PASSED_USERNAME whereas in some other places you have used it with the class named prefixed UniteActivity.PASSED_USERNAME. Since it is a Public Static Constant always use it prefixed with the class name.

How do I use Startactivity and leave the calling Cocos2d Layer active (Android/Java)?

I'm trying to port my iPhone Cocos2d game to Android using the Cosos2d for Android framework (Java).
I'm running into a problem when I start PreferenceActivity from my main activity. This is my setup:
(1) HelloWordActivity.java:
Inits the Cocos2d environment and starts GameLayer.scene
(2) Gamelayer.scene:
Presents the user with the game but also has a button to show the settingspane which gets called from the Gamelayer class like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(helloworldactivity, ShowSettingsActivity.class.getName());
helloworldactivity.startActivity(intent);
This shows the Preference Activity just fine, but when closing the preferences and returning to my game it appears it did not persist (it get's reloaded/restarted and does not retain it's state).
How can I present the user with a standard Android preferences screen without destroying the current game session in Cocos2d ?
from the names of your classes , I'm assuming you used Dan's tutorial as a starter
make sure you remove dan's onStop() override method , that gets called when you are loading the preferences and it calls
CCDirector.sharedDirector().end();
removing this should do it for you
I did notice that you gave up and switched to andEngine , that's a nice engine too but I thought I'd answer this one anyway
You need to get the main activity to start new activity, I hope this code will help you
Activity context = CCDirector.sharedDirector().getActivity();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(helloworldactivity, ShowSettingsActivity.class.getName());
context.startActivity(intent);

Categories

Resources