I need some help creating a properties page in Android.
I have created an Activity and have a TableLayout with a title, 5 properties that include a Spinner to select a property class and a EditText that the user can type into, followed by a couple of buttons reading Accept and Cancel.
I am launching the activity by creating a new Intent with that class and using a StartActivityForResult, where I need to Bundle all of the properties back to my main activity. I have an OnActivityResult defined in the main class. I am having difficulty in the properties page on how to return the activity result from the button callback and how to package and return the Bundle. Any not so simple examples would be appreciated.
on the Activity that is returning the result you would do something like this
Intent i = new Intent();
i.putExtra("value_a", someValue);
i.putExtra("value_b", anothervalue);
setResult(RESULT_OK, i);
finish();
then in the activity that is waiting for the result you would do something like this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) // 0 would be whatever id you gave this when you started the activity for result
{
Bundle extras = data.getExtras();
String property_a = extras.getString("value_a");
String property_b = extras.getString("value_b");
// ... whatever else you need to with the results, maybe they are not strings...??
}
}
You can use Preference Activity to store yours application settings
Related
How can I send data to another Activity that did not startActivityForResult?
I Override the onActivityResult method in order to perform logic as soon as a result returns from another Activity. Specifically, from my EditItemActivity
Process:
Within my MainActivity,
when I click on an item in the ListView, I am redirected to another Activity called ToDoDetailActivity
When I click the Edit button I am redirector to another Activity called EditItemActivity"
As soon as I make my changes and press the Edit button on the EditItemActivity, I return back to the MainActivity
data = new Intent(EditItemActivity.this, MainActivity.class);
data.putExtra(EDITTEXT_VALUE, etValue);
setResult(123, data);
startActivity(data);
Within my MainActivity, I want to UPDATE the item I just edited with this logic within my onActivityResult method. However, I don't see any logs in LogCat meaning I do not believe this method is being used and therefore no logic is performed
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
return;
}
if (resultCode == 123) {
String editedItemValue = data.getStringExtra(EditItemActivity.EDITTEXT_VALUE);
todoItems.remove(editedPosition);
aToDoAdapter.insert(editedItemValue, editedPosition);
aToDoAdapter.notifyDataSetChanged();
}
}
In your code:
data = new Intent(EditItemActivity.this, MainActivity.class);
data.putExtra(EDITTEXT_VALUE, etValue);
setResult(123, data);
startActivity(data);
When you execute startActivity with MainActivity.class it is launching a new Activity so you are not going to find a result in "onActivityResult" method. It is because you are not returning to the main activity after finishing in a "child" activity.
What I have done in the past is use broadcast event to notify the "MainActivity" about updating the view.
Hope that helps.
We can use BroadcastReceiver or use database to store the result of your edit in EditItemActivity then update that result in onResume method of MainActivity
In my app, there are 3 activities and their calling sequence is like this...
Splash activity--->Dashboard Activity--->List Activity
If it goes like this then If I press back button on List activity then it will follow the reverse sequence of above.
There is one requirement where user can navigate to List Activity directly from Splash (Skipping Dashboard Activity) Now when user will hit back button on List Activity then I wan't to show Dashboard Activity which is not there in the Activity Stack.
So please help me with the best approach.
Pass a boolean through the intent for going to List Activity from either of the others. Using onBackPressed check if the boolean is true or false for skipping Dashboard Activity.
Then if true put new intent for loading dashboard activity and finish(); on list activity.
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can directly go to splash from list Activity while going to
ListActivity from Dashboard Activity call finish()
Intent i = new Intent(DashboardActivity.this,ListActivity);
startActivtiy(i);
finish();
Start your inner activities with startAcvitiyForResult
Intent i = new Intent(this, Activity_2.class);
startActivityForResult(i, 1);
and in your inner activity
#Override
public void onBackPressed ()
{
finish();
}
You can also do stuff in your outer activity as you like after your inner activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == 1){
Log.i("TEST", "RESULT_OK");
}
else {
return;
}
}
}
Having a little trouble when trying to return a byte array from an activity. The return code is:
private void returnLocation(byte[] mapImage) {
Intent intent = new Intent();
intent.putExtra("mapImage", mapImage);
setResult(RESULT_OK, intent);
finish();
}
EDIT: This is how I call and try to receive it in the parent activity:
....
Intent i = new Intent(getActivity(), ChildActivity.class);
ParentFragment.this.startActivityForResult(i, 255);
...
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 255) {
if(resultCode == Activity.RESULT_OK) {
mImage = data.getByteArrayExtra("mapImage");
}
}
}
I've a breakpoint on the onActiviyResult that never gets hit when i try returning a byte array.
However, when trying to return the intent it goes back to the first activity (not the one that called it). I can't see anything that suggests a problem in the logger.
If I change the Extra value to '5' (an integer), it returns fine.
Is there something I'm missing when trying to pass back a byte array?
Thanks
Android has a limit on the size of a Bundle, which is what the Intent uses internally. If the data exceeds the bundle size it will display ERROR/JavaBinder(7881): !!! FAILED BINDER TRANSACTION !! in the logs, but it won't throw an exception.
Try writing the byte array to a temp file and passing the file name in the Intent.
I figured that you are calling startActivityForResult(i, 255) from a Fragment.
Instead of calling ParentFragment.this.startActivityForResult(i, 255);
you should call : startActivityForResult(i, 255);
If the above approach doesnt work, try the following:
Since the Activity that is hosting your fragment gets onActivityResult() result so you need to override activity's onActivityResult(), call super.onActivityResult() in your fragment to propagate to respective fragment for unhandled results codes or for all.
I'm building a small app that requiere authentication. In my main activity I have a Parcelable class named "user" that contains the username and password of an user, when a user click on a button it starts a new activity passing that user class. It works like a charm, in the child activity the user fill the form to authenticate, then when user press the back button, I would like to send the "user" class back to my main activity.
Is it possible to do that ??
Start your child activity with:
startActivityForResult(startIntent, 1);
In your child activity, intercept the back button and append your data:
#Override
public void onBackPressed() {
Intent data = new Intent();
data.putExtra("key", yourDataHere);
setResult(Activity.RESULT_OK, data);
super.onBackPressed();
}
And get data inside parent activity inside onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
DataType yourData = (DataType) data.getParcelableExtra("key");
//Do whatever you want with yourData
}
}
I am new to android so please excuse the newbie question. I have a game I am trying to port from an old Java applet to android. My goal is to get this functional and then post an article on a site like CodeProject (or a better one if there are ones more appropriate). The idea is to show that a person brand new to android development can create an app in a reasonable amount of time.
I am making some progress but have run into a problem. I have the main activity in which the user interacts with. I then created a menu item that in turn starts a second activity (call it child) with a modest number of checkbox's, seekbar's etc to fill in parameters. I can successfully pass the class containing all the options from main to child. But I cannot get the child to pass this data back to the main.
First here is my main code that starts the child activity:
public void addBalls()
{
Intent myIntent = new Intent(this, GameOptions.class);
Bundle b = new Bundle();
b.putSerializable("options", gameParams);
myIntent.putExtras(b);
startActivityForResult(myIntent,STATIC_OPTIONS_VALUE);
}
The data passed to the child (and hopefully back again) is:
public class GameOptionParams implements Serializable
{
private static final long serialVersionUID = 1L;
public int speedBarPosition;
public int vgravityBarPosition;
public int mgravityBarPosition;
public int viscosityBarPosition;
public int restititionBarPosition;
public boolean trace;
public boolean collide;
public boolean mush;
public boolean wrap;
public boolean flicker;
}
And here is the expected return (again in main)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case (STATIC_OPTIONS_VALUE) :
{
if (resultCode == Activity.RESULT_OK)
{
//retrieve intended options
Bundle b = data.getExtras();
gameParams = (GameOptionParams) b.getSerializable("options");
}
break;
}
}
}
The child activity successfully receives the gameParams data. It then interacts with the user to update the values and then I attempt to return it but it does not seem to get sent to main. Here is the child code in the onStop() override.
Maybe this code should not be in the onStop() override but I can't determine where else to place it.
#Override
public void onStop()
{
super.onStop();
//read widget values
gameParams.speedBarPosition = speedBar.GetPosition();
gameParams.vgravityBarPosition = vgravityBar.GetPosition();
gameParams.mgravityBarPosition = mgravityBar.GetPosition();
gameParams.viscosityBarPosition = viscosityBar.GetPosition();
gameParams.restititionBarPosition = restititionBar.GetPosition();
//todo save to persistent
Intent resultIntent = new Intent(this, TiltBall2ImpactActivity.class);
Bundle b = new Bundle();
b.putSerializable("options", gameParams);
resultIntent.putExtras(b);
setResult(Activity.RESULT_OK, resultIntent);
}
Back in the main onActivityResult override I always see requestCode=0, resultCode=0, data=null. I assume this is a typical newbie problem, I have been reading the sdk documentation, user forums etc and have come close to a solution but just not quite there yet. Any help would be appreciated.
Since this is sort of a setting menu for the game, I assume you are going to need these values for more than one activity. If so you extend the android.app.Application class.
In that class you can create attributes to hold your values. In any activity you can call
MyApplication myApp = (MyApplication)getApplicationContext();
where myApp is a singleton. So you will get the values you set from another activity.
You will need to add this code to your application tag in the manifest file for it to work
android:name=".MyApplication"
If you need to keep these values for next startup of the application, you need to use SharedPreferences. This is a good tutorial for that
http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html
Assuming in your 'child' activity, the user has to press an 'OK' or 'Save' button then put the code to set the gameParams parameters in the button's onClick(...) handler.
Use the default constructor for instantiating the Intent, example...
Intent resultIntent = new Intent();
...then after creating the Bundle and adding gameParams to it and calling setResult(...), simply call finish() to terminate the 'child' activity. There aren't many occasions that I can think of to override onStop() and I suspect you don't want to be using it to attempt returning the Intent.