Creating/Rendering Activity before starting it - android

We use different activities to navigate through our app. One of them is very complex and contains a lot of nested views/images etc, so when I usestartActivity(intent1) in the activity before it, there is a short delay and it feels/looks laggy.
All the information needed to create the content views is known in advance.
So my question is: is there a smart way to prerender/preload the activity or its content view?
As i figured the intend only holds information about the next activity but no instance of the activity itself, so i assume there is no way to tell the intend to create the activity before i call the startMethod.
One idea i hat was to create a static view before starting the activity and set this view as contentView in the onCreate() method. But it seems like a bad hack to me.
Thanks in advance!

The best solution would be not to start a completely new activity but using a ViewPager or ViewFlipper. Switching between Views should be then nearly instantaneous and you also get the chance to easily apply animations.
If that is not possible you could start a new activity but put a ViewSwitcher in there. The first View would be a progress bar. The second view is inflated and added to the Switcher in a background thread.

Related

How to get the parameters(like Width and Height) of a view of an fragment to a Activity?

I'm beginner to android and currently I'm working on a small project.
In my project I have a fragment with text and imageview, and in my Main activity I have a button and imageview.
When I press the button in my activity class it opens up the fragment, but I want to animate(Move) the the imageview in main activity to the position of the imageview present in the fragment.
Is there any simple way of getting the position of the views present in the fragments to activity class?
I'm stuck in this situation from few hours. Please help me.
What you are attempting is difficult because that is not how you are supposed to do it. Fragments are supposed to be self contained units, completely modular and interchangeable. On the other hand Activities are just supposed to be empty containers for Fragments. All the logic and UI has to be contained in the Fragments them selves and the Activities are supposed to be used to arrange and display those Fragments. Nothing from outside a Fragment should have anything to do with something inside the Fragment. So if you restructure your app with that in mind you will find that everything will be MUCH simpler.
A few pointers:
Move all of the UI to the Fragment
It is completely fine to perform FragmentTransactions from inside a Fragment. You don't have to take a detour through the Activity.
Try to understand the difference between Fragments and Activities and don't let yourself be mislead by tutorials which don't adhere to this separation. Most Android tutorials on the internet are outdated and wrong. Refer to the official tutorials here.

passing objects to another activity

Situation
In my alarm clock app I start a NewAlarmActivity by clicking a button.
After the user has made all the selections, alarm gets set and `finish() is called.
The matter
I create a new LinearLayout object with images and text within it. That layout must be added to the screen of previous activity (to another LinearLayout placed inside a ScrollView) so the user is able to see the alarm set. Somehow I have to pass that LinearLayout object to the first activity and tell it to receive and add the object to the screen.
How can I do that?
You just need to read some documentation about startActivityForResult().
BTW, IMHO, you should not transport your LinearLayout object between activities, that's ugly.
How to manage `startActivityForResult` on Android?
Starting Activity And Getting Result
This has been asked countless times...
No You don't have to do this. Try the approach of storing your data into a database and then when going into that Activity, build your layout according to the data you have. This is a much better way than passing a layout from one Activity to antoher.

Views communicating with fragments

I am trying to make a sudoku application. It's a fragment based design, in which a fragment hosts a custom view which is a board. I am trying to learn how to build an effective communication within FragmentActivity, Fragment and View
Although a view is created using the FragmentActivity context and I can catch a reference to that context within the current view and then call methods inside FragmentActivity I don't want to tie views so directly to a fragment activity. Instead I want to tie the view to use methods inside a fragment. How can i do that, I can I capture a reference to a fragment and call methods inside that fragment from a view?
First of all, I strongly recommend that you read through the documentation on Fragments, as you clearly don't understand the whole concept/purpose of using Fragments in the first place (which is OK, because they are confusing the first time you learn them :P).
A lot of your question doesn't make very much sense to me, because I'm not sure what the View you are speaking of refers to. What I can tell you is that Fragments have their own UI/layout, along with their own separate lifecycle. So it sounds like you don't want your FragmentActivity to interact with the Fragments layouts/methods at all... instead, you should implement the UI's behavior and layout inside of the Fragment itself. That is, the Fragment will be in charge of updating the UI, receiving click/touch events, displaying information on the screen etc. The FragmentActivity will simply hold a reference to the current Fragment(s), and will be in charge of displaying/swapping in and out new Fragments as necessary (via the activity's FragmentManager).
Hope that answers the question somewhat... read through the documentation a couple times, it sounds like you are just misunderstanding the theory/purpose behind Fragments.

Converting Multiple Activites into a Single Fragment

I've recently decided to update my app to support the new fragments feature in honeycomb 3.0.
My Application currently works on a list view that opens different activities depending on which list item is clicked.
Using an adaptation of the code in this tutorial I have created an app that consists of only two activities, but depending on which list item is clicked the second "viewer" activity launches using a different layout xml.
Unfortunately I haven't been able to figure out how to call the old methods that had all the functionality. Should I Import all of my old activities and then call the methods into the viewer activity (I may need some advice on how exactly to do this) or should I just put all the methods directly into the same viewer activity (please consider the size of these methods(which is very large by the way)).
Once everything is working with two activities upfront then it will be a pretty simple task of "fragmenting" the app as demonstrated here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
Thanks
As James has pointed out you will have to move the business logic from your Activities to your Fragments.
To handle events you can create a listener Interface. The CONTAINER activity/ies will implement this interface. As fragments has access to the container activity you will be able to delegate to the container Activity the "logic" for the desired events. For this events the activity will decide whether to launch a new activity, show/hide new fragments or whatever.
I had a similar question, take a look to the question and answer: here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
I think its possible to allow multiple fragments to occupy the same space in an activity. Again, take a look to the answer here ... I think the concept/scope of Activity has change a bit and now an Activity can contain different Fragments which every one will allow user to do a single focused thing.
I'm not sure what you mean by "call the old methods that had all the functionality". You'll want to rewrite all of your activity classes as fragments. Check out this tutorial here (it's very concise). Basically, you'll want an activity that consists of a ListFragment and a FrameLayout. Your ListFragment will update the FrameLayout by changing to the appropriate Fragment based on which row was selected.

Sending context/activity? Leaks? - Android

I'm having an application with different views, and when I'm "flinging" at right or left another view pops in and the other slides out. Most of the application is based in that activity.
Since every view has buttons and lists (which you are supposed to interact with), the activity class is getting rather big and I was thinking of separating it. But which is the best way to do that?
Should I create classes that gets the activity context so I can declare the buttons value (findviewbyid()) etc. or is there another way to do this.
Is there a danger in sending the activity context? Is getApplicationContext() better to use for a time like this?, or should I just keep working on the class which is getting quite big?
Maybe there is a better way to do this?
I think the best way to do this would be to create a class that extends the View, and put all your child views in there. You'll still have to pass your context, but this way it'll be managed properly by the parent class.
I had a similar dilemma:
Safe to pass instance of an Activity to another object?

Categories

Resources