For example
Intent intent = new Intent(this, SecondActivity.class);
eclipse error: The method setClass(Context, Class) in the type
Intent is not applicable for the arguments (FirstActivity.ClickEvent,
Class)
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
But that will be correct.
Anybody can explain the difference between those two ?
Thanks.
this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to.
Activity.this points to the instance of the Activity you are currently in.
Shubhayu's answer is correct, but I just want to make clear for anyone who see this question that this and Activity.this is the same if you are using it directly in the activity.
This is answered here
Example:
#Override
protected void onResume() {
super.onResume();
Log.d("Test", this.toString());
Log.d("Test", MainActivity.this.toString());
}
Result:
D/Test: com.example.app.MainActivity#e923587
D/Test: com.example.app.MainActivity#e923587
When you are pointing to this inside click event, it is pointing to the click listener.
You are intent to transfer control from one activity to another and for that u ll have to specify an event basically and hence the error.
this means the entire activity and firstactivity.this means an event occurring for example a a button clicked.........
Related
Is the function onAdDismissedFullScreenContent() working when the user closes the add or it works when the ad did not appear? I do not understand very well the meaning of dismissed in this context. The second question is, is it mandatory to put inside this onAdDismissedFullScreenContent function the following code? What is the intention of the following code? Could I put nothing on this function? Thanks in advance
onAdDismissedFullScreenContent(){
Intent intent = new Intent(getApplicationContext(), Activity.class);
startActivity(intent);
}
For the first question
Yes, this method onAdDismissedFullScreenContent() is user closes the full screen ad content.
And if the ad failed to show full screen content the method
onAdFailedToShowFullScreenContent(AdError adError) will be called.
You can check the public method summery for FullScreenContentCallback here
And for your second question,
No, it's not mandatory to set anything inside these functions.
and the code
Intent intent = new Intent(getApplicationContext(), ActivityWorkout.class);
startActivity(intent);
will open an activity called ActivityWorkout when the ad is closed.
I hope I answered your questions, Happy coding.
Intent intent = new Intent(currentActivity, home.class);
currentActivity.startActivity(intent);
I have this issue that I have a list of A and I can add new A, so I startactivityforresult activity with form for A and there is also a button for selecting AType, so I startactivityforresult another activity with AType list.
Problem comes when I do post back result in AType list.
Intent intent = new Intent();
intent.putExtra("aType", aType);
setResult(RESULT_OK, intent);
finish();
And the result is pushed back to A List instead of A Form.
Is this normal behavior or I am doing something wrong?
If this is normal behavior, what can I do to push back result to A Form instead of A List.
If you need more code - I will provide, but I find it now irrelevant.
I have found out that A Form finishes after startactivityforresult call. But why?
Activity1:
Intent intent-=new Intent(this,myclass.class);
startActivityforResult(intent,100);
Override the method OnACtivity result in Activity 1
Activity2:
setResult(RESULT_OK);
finish();
Turned out I have noHistory="true" in AndroidManifest.xml for activity A Form, that is why it was going back to A List Activity
I'm curious as to whether an intent is unique to a set activity or not.
For example, my current usage of an intent is to give it some data from Activity 1 and pass it on to Activity 2.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("id", id.getText().toString());
intent.putExtra("weight", weight.getText().toString());
intent.putExtra("zipTo", zipTo.getText().toString());
intent.putExtra("locationId", locationID.getSelectedItem().toString());
intent.putExtra("imageNum", 1);
startActivity(intent);
Now, in MainActivity I can use the following code to retrieve the data passed from the previous activity.
Bundle bundle = getIntent().getExtras();
id = bundle.getString("id");
weight = bundle.getString("weight");
locationID = bundle.getString("locationID");
zipTo = bundle.getString("zipTo");
After doing some work in MainActivity, I need to return to the previous activity that sent us to MainActivity (and I need the data that was originally sent to return).
Do I need to redefine an intent, and do "intent.putExtra" for each value again in MainActivity before sending it to the first Activity? Or is the intent global, and once defined in one activity I can use it in the others with getIntent().getExtras()?
Intents are not global. You only have access to the bundle in the activity you started with the intent passed.
You should use startActivityForResult(intent). This provides the behavior youre looking for.
Getting a result from an activity
The idea is you start the new intent. In this case 'MainActivity'. Once you are done with your logic in MainActivity you use the strategy above to pass a result back to the activity that started MainActivity.
information passed through intents are not global from one activity to another, what is global is shared preferences and information that is metadata which is stored in manifest.xml.
I created my Base activity in separated process by adding to it's <activity> fragment in manifest:
android:process=":login_exception_process"
In Base activity I initiate my connection objects, and start next Activitty. But from another Activity when I try to get this instances of connection objects they are NULL.
Objects initiated by Base activity are saved in separated classes in static variables. How can I access this variable?
Passing values to another Activity
You have to start your next activity sending an extra in an Intent.
Intent intent = new Intent(this, NextActivity.class);
intent.putextra("keyName","value");
startActivity(intent);
And your next Activity, you can retrieve this value by doing this:
String data = getIntent().getExtras().getString("keyName");
Here is the documentation for that.
Getting a result from an Activity
Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. For example, if you want to start the NextActivity and return a value back from NextActivity to BaseActivity, you can use startActivityForResult().
Here is the documentation for that.
Update
Sending objects via intent can be done with two options:
Pseudo-codes
Serializable
intent.putExtra("MyClass", your_object);
// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Parcelable
Intent mIntent = new Intent(this, NextActivity.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, your_object);
mIntent.putExtras(mBundle);
startActivity(mIntent);
Despite Serializable seems to be the easiest option, follow this tutorial it will teach you both ways.
Hope this helps you.
You should use Extras
How do I get extra data from intent on Android?
Basically, when creating your intent you have to put extras:
intent.putExtra("name", "My name is John");
then when you want to get it back when you start the next activity:
this.getIntent().getExtras().getString("name");
You can do this with any type if you convert the Object to a byte array.
I have tried a lot of different ways to use the same object in different classes. I want to do this because I want the data to be stored in this object.
But no matter what I do, it hust wont be stored when I press the "back" button on the phone and then go back.
I am sending a bundle with the object to the second activity like this:
Intent intent = new Intent(this, TrainingDays.class);
Bundle bundle = new Bundle();
bundle.putSerializable("programObject", program);
intent.putExtras(bundle);
startActivity(intent);
And I am receiving the object like this:
Bundle bundle = this.getIntent().getExtras();
program = (Program) bundle.getSerializable("programObject");
I can use the object just fine, but I every time I switch back to another activity, the object is clean from data. Am I doing something wrong here or could it be something else that causes this? Any help will be greatly appreciated.
That's the correct way to manage objects. If you do not need to write the object on a File I would suggest to switch to Parcelable. About your problem, if you want to pass back the object, you can use the pair startActivityForResult and onActivityResult
Override the onBackPressed() method of Activity and use #blackbelt 's combination of startActivityForResult() and onActivityResult()
You can see an example on the developer page http://developer.android.com/reference/android/app/Activity.html#StartingActivities
onBackPressed() controls going back to the previous activity you could create a new Intent in the onBackPressed(), then a create a Bundle and Put the Bundle in the setResult() method as such setResult(RESULT_OK, mBundle); and Bundle Object will get passed back the previous activity.
in Short
#Override
public void onBackPressed(){
Intent data = new Intent();
Bundle newBundle = new Bundle();
newBundle.putParcelable("MyObject", mObject);// must extend Parcelable on Object class
data.putBundleExtra("ObjectBundle", newBundle);
setResult(RESULT_OK, data);
finish();
}