Transfer ListView with intent - android

I want to fill ("calculate") a ListView in a Loading Activity and when it's ready, start the new Activity with the ListView filled. So I don't have to wait to fill it in the second activity. I already have the code to fill it, but it's in the second activity.
Thanks

First, this is not really possible. You cannot readily transfer widgets or adapters to another activity.
Second, this is not really necessary. Creating a ListView is fairly cheap. If you are experiencing performance anxiety, use Traceview to determine where the problem is, then scope your plan to deal with the problem. For example, perhaps it is your model data, not the ListView itself, that you need to load in advance. Or, perhaps you just need to move some work to background threads, such as loading images to go into the ListView rows.
Third, if these two bits of UI are this closely coupled, they should not be separate activities, but rather one activity. Whether you use fragments, or hide/show widgets, or whatever, is up to you.

Your code for Activities, User Interface that may contain creating custom views , AND code for adapters must be independently defined or in separate packages if u prefer to go for clean coding having done that u will not face above problem and also u can REUSE your code .

What you really want is to transfer the data from one activity to another activity. Instead of passing the "ListView", you should be passing the data that you need to populate in the other activity
Intent intent = new Intent(CurrentActivity.this, OtherActivity.class);
intent.putExtra("SOME_KEY", mySerializableData);
startActivity(intent)
You can pass any type of serializable data using intents.
So you should ideally:
Create your adapter somewhere outside of your CurrentActivity and
OtherActivity
Pass the mySerializableData from CurrentActivity to OtherActivity
Use the same adapter to populate the ListView using the data passed in the intent using getIntent().getSerializableExtra()
Use instanceof to ensure that what getExtras() returned is of the type MySerializableData
So for example if we want to pass a ArrayList<MySerializableData>
Object obj = getIntent().getSerializableExtra("SOME_KEY");
if (obj instanceof List) {
//Do stuff
}

Related

How to transfer data from one activity to another in android studio without opening the new activity?

I am trying to create a recycler view with data from another activity. I need to transfer data from one activity to the recycler view activity without it opening the recycler view activity. Because only when I press another button do I want to go to the next activity.
I have tried using intent, however startActivity(intent) always opens the other activity. I also tried using Share preferences to save the data and retrieve it in the other activity. However, Base64.DEFAULT was not working for me.
public void sendData(Bitmap images, String image_class) {
Intent send = new Intent(getApplicationContext(), Scanned_List_Activity.class);
send.putExtra("image_class",image_class);
send.putExtra("image", images);
startActivity(send);
}
If I understand your question right, you should do it differently.
Activity represents a View or better to say a Controller in the MVC architecture. Data you gained in the RecyclerReceiver comes from a model, activity just passes it to a view. That's why the right way is to have this data in the model layer rather than pass it over an Intent as a Parcelable object.
Nowadays we have a ViewModel class which is a layer between a model and your view. If you use DI tool like dagger you can keep a single instance of your ViewModel between this two activities - when you prepare your dataset on the first activity, the second activity will get it next time when it would be opened.
In short you should put this dataset either in the separate table and request from 2nd activity or to put on the ViewModel which single instance is shared between this two activities.

How do I pass a selected object from a list activity to a details activity

Sorry if this is obvious but I am very new to andriod (just playing around trying to learn something new).
I think this is a fairly common thing to do, but I am having trouble figuring out the best way to do it.
Right now I have 3 activities. A main activity that will start a second activity that shows a list of objects, and finally a third activity, started from the list activity, where I want to make changes to the selected object. Both the list and details activities host fragments.
Is there some safe place I store the selected object and retrieve it from the details activity/fragment? It seems like the main activity is the only common link, but I am not sure I can count on that activity not having been destroyed.
Do I need to pass it via an Intent? This seems it would work but also seems overly complicated. (I am thinking I have to serialize the object and return it as a result of the activity)
Should I just combine the final 2 list and details activities into one activity, and swap out the fragments? I think if I do that I can safely store the selected object in the combined activity, is that correct?
The common and the right way to do it:
You have to create an object (that you will pass) and implement Parcelable interface (e.g.) for that object and then pass it with your intent intent.putExtra("identifier", youParcelable)
Example:
Add an object while starting new Activity:
Intent intent = new Intent(getApplicationContext(),
yourActivityClass.class);
intent.putExtra("identifier", youParcelable);
startActivity(intent);
Retrieve object from intent in newly opened Activity:
Object object = getIntent().getExtras().getParcelable("identifier");
Source

Passing pointer to Activity to next Activity

I'm very new to Android programming (and Java for that matter) coming from an iOS background.
What I am trying to do, is pass a pointer to a Fragment from one Activity to another.
Basically, I have a starting activity called BeginActivity that handles a couple of Fragments for login and register screens. Once logged in, I load up the main activity of the app called TabsFragmentActivity using this code:
public void loggedIn() {
Intent intent = new Intent(this, TabsFragmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
finish();
}
I'm using FLAG_ACTIVITY_CLEAR_TOP as I dont want the user to go back without actually logging out first.
Now the problem:
In BeginActivity I have a pointer to a fragment that holds the users data. I am using it like a singleton, that the first few view fragments can access from BeginActivity.
I need to pass this same object to the new TabsFragmentActivity before I call finish() on it.
How do I do this?
I know I can use putExtra() but I believe that is just for strings etc.. and not other Fragments.
Is there a way in the newly created TabsFragmentActivity that I can reference the BeginActivity to 'grab' the pointer?
Thanks
First of all, you should be sure about Fragments and Activity life cycle.
Fragments are designed to be reusable UI complex components. They look like activity, but you can reuse. So,you can have as many activities you need containing the same fragments, but not the same instances of these fragments.
If you just want to pass you user data for another activity you must use Bundle and putExtra(). Depending of the user data type can be necessary implements Serializable or Parcelable Interfaces, as #gheese said.
If you want to use the same UI appearence of your fragment on two or more activities, besides use Bundle and putExtra. Each activity that you want this behavior must contains a field whose is a Fragment and in the moment of starting this fragment you can use getActivity().getIntent().getExtra to get the user information and populate your fragment.
Basically you need to be able to pass your class via the intent, look at Serializable / Parcelable interfaces
This question has the answer you require
How to pass an object from one activity to another on Android

How to fill the ListView from another class?

My problem is follow (I know that selected structure of my classes looks like bizarre, but ...).
I have the MainActivity with inner class, that extends AsyncTask to parse HTML from internet.
This activity parses HTML-page and fills the ListView in AnotherActivity.
AnotherActivity must call the inner AsyncTask and PostExecute must fill the ListView by another data (the sense of this actions that AnotherActivity is results of search and another search means the going to new page of search's results of YouTube)
I wrote the next code in PostExecute of AsyncTask:
where I get Activity with ListView, create adapter and assign it to ListView.
ResultsQueryActivity resultsActivity = new ResultsQueryActivity();
ResourceDataAdapter resourceDataAdapter = new ResourceDataAdapter(resultsActivity,MainActivity.listVideosData);
resultsActivity.setListAdapter(resourceDataAdapter);
After execution of this I get the message
*02-01 07:40:56.784: E/AndroidRuntime(525): java.lang.IllegalStateException: System services not available to Activities before onCreate()*
I really have no idea how to resolve it. Thanks for help!
Why do you have two activities, when both of them seem need to be done on the same activity?
Just use one activity. When parsing is done, update the ListView with your data, then show the ListView. Initially the ListView would be hidden. Your other views (if any) would be hidden as well.
Or use a ListActivity, doesn't really make a difference.
I would guess it has something to do with the way you are creating ResultsQueryActivity... Typically you would start it via an Intent and pass all your video data to it by putting it into a Bundle. Try replacing your code in postExecute() with something like this (assuming your video data is a String array):
Intent intent = new Intent(MainActivity.this, ResultsQueryActivity.class);
intent.putExtra("videodata", MainActivity.listVideosData);
startActivity(intent);
Then in the ResultsQueryActivity class, you can retrieve the data in the onCreate(Bundle b) method:
Bundle extras = getIntent().getExtras();
String[] videodata = extras.getStringArray("videodata");
You cannot instantiate a activity, activities is managed by system.
The flow of the two activity is not very well.
If MainActivity is only used to parse html, then it should not exist. Put the the asynctask in AnotherActivity and fill the list in postExecute.
If MainActivity must exist, then you should not fill data in the task. Try to parse the data to AnotherActivity, and let it fill data by itself.

When to use more Activities

I have an Activity which is an OpenGL view. I also have an xml layout to use for preferences. Until now, to show the preference menu, I just brought it to front by setContentView(). And the same to get back to the OpenGL view.
But is this a case where I should give the preference menu its own Activity?
I guess this would make a few things much easier. For example, the back button would just work, opposed to now where I have to code it or it will just exits the application.
And if this is a good idea, how do I pass data both ways? I have a class that store all preferences. Can I send it to the Activity and back again? Or is the best way to store the preferences in a sqlite database and then use it for passing data?
I find it easier to segregate menus and such into separate activities (unless you are using dialogs etc..) As far as storing data you can do it a number of ways:
Database
StoredPreferences
Intent extras with putExtra/Bundle
Creating an application subclass and storing preferences there
Each have their merit. 4 is pretty easy as you just have to state the application class name in your manifest then call: MyAppClass app = (MyAppClass)getApplicationContext(); and you can then use any variables in MyAppClass via app. 2 is also straightforward.
You already pointed out the main difference: history management.
You can pass data to Activity via Intents putExtra()/getExtra():
Create an Intend and add custom data via Intent.putExtra(..)
Start the new Activity: startActivityForResult(intent).
Inside new Activity you can get extra data with intent.getXyzExtra() (where xyz is type).
When new Activity is done just call setResult(int, resultIntent). Again you can add extra data as described in 1.
Call finish() to end the activity.
In original Activity method onActivityResult will be called. Again extract data from Intent as described in 3.

Categories

Resources