Asked  7 Months ago    Answers:  5   Viewed   26 times

What's the correct way to pass a bundle to the activity that is being launched from the current one? Shared properties?

 Answers

27

You have a few options:

1) Use the Bundle from the Intent:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

2) Create a new Bundle

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

3) Use the putExtra() shortcut method of the Intent

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);


Then, in the launched Activity, you would read them via:

String value = getIntent().getExtras().getString(key)

NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.

Tuesday, June 1, 2021
 
Fanda
answered 7 Months ago
11

If your app hasn't been "terminated" then #1 should already work and #2 just requires saving any values that aren't managed automagically into the Bundle in onSaveInstanceState() then restoring them in onRestoreInstanceState().

This is kind of a hack, but I think your best option for #1 in the case of the app actually being terminated would be to save the most recent Activity in the onResume of each of your Activity classes then when you first run the onCreate of your first activity do a check then start the correct Activity... maybe even put in a blank Activity at the beginning. Something like this:

StartActivity:

public class StartActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get last open Activity
        String lastActivity = PreferenceManager.getDefaultSharedPreferences(this).getString("last_activity", "");
        if (last_activity == MyActivity2.getSimpleName()) {
            startActivityForResult(new Intent(this, MyActivity2.class));
        } else if (last_activity == MyActivity3.getSimpleName()) {
            startActivityForResult(new Intent(this, MyActivity3.class));
        } else {
            // assume default activity
            startActivityForResult(new Intent(this, MyActivity1.class));
        }
    }

    public void onActivityResult() {
        // kill the activity if they go "back" to here
        finish();
    }
}

Then in all the other Activities (MyActivity1,2,3) save the values like so:

@Override
public void onResume() {
    Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit();
    e.putString("last_activity", getClass().getSimpleName());
    e.commit();

    super.onResume();
}

You'll also have to handle saving /restoring the data for each Activity manually. You could save all the values you need into the preferences inside the onPause() of each of the Activities then restore it in the onResume().

Thursday, July 29, 2021
 
toesslab
answered 5 Months ago
31

When constructing your class, you can have a constructor that takes in a Context and assigns it a local Context object within your class.

public class LocationFinder {
     private Context myContext;
     private Geocoder geocoder;

     public LocationFinder(Context context)
     {
         myContext = context;
         geocoder = new Geocoder(myContext);
     }

}

And then when you try to access this class, make sure you initialise it like:

public class TestActivity extends Activity {
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          LocationFinder lFinder = new LocationFinder(getApplication());
     }
}

Of course, you can't access a context from every class that you will be running. So a reference to a View can suffice.

LocationFinder lFinder = new LocationFinder(anyView.getApplication());
Sunday, September 26, 2021
 
tuckerjt07
answered 3 Months ago
58

It's not working because f only exists in the context of your controller action.

When a user clicks your link, a new HTTP request is made for your javascript. The controller action handling this request is different from the one that rendered the page, so you need to set f in this controller action and pass it as a local to addRoundLink.js.erb.

Example code

  • I'm assuming f is determine by your model object/instance.

In your controller

   def add_round
        model = MyModel.find params[:id]
        f = model.determine_f

        render :partial => 'add_round', :locals => { :f => f }
   end

In your new.html.erb

<%= link_to "Add Round", add_round_path(@model), :remote => true %>
Monday, October 18, 2021
 
Mateusz Pryczkowski
answered 2 Months ago
90

You have to use javascript to manipulate a hidden field tag that will be submitted in the form.

Form :

<%= hidden_field_tag "image_url" %>

JS :

<script type="text/javascript">
    var imageURL;
    var sCode = function () {
        $("#qr").html("<p align='center'><img  src='https://chart.googleapis.com/chart?  
        chs=250x250&cht=qr&chl=123&choe=UTF-8'/></p>"); 
        imageURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=123&choe=UTF-8";
        $('#image_url').val(imageURL);
    }
</script>

Of course you'll have to execute the function declared in sCode somehow.

In the controller, just access params[:image_url] to get the URL

Wednesday, December 8, 2021
 
Jan Gassen
answered 1 Day ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share