I am having trouble with ASYNCTASK in my fragment - java

I'm new to fragments.I am trying to call search API in my fragment using AsyncTask. Json Parsing is done in MainActivity, by the way I'm not in my main activity when I use this fragment, it's connected to another activity. It works, but I'm having trouble passing the parsed info that I stored into a bundle to a new fragment. To be specific it crashes the moment I try to open a new fragment. I have the parsed information and I've set the arguments for my fragment, it's just the moment I use .commit() it crashes.

It would be great if you could post the piece of your code so we can check what is the issue.
Although I have deduced from your explanation that you might be saving the information in doInBackground which runs on the background thread. Instead, you could pass that info to the onPostExecute and save it into SharedPreferences since onPostExecute runs on the UI thread.
Let me know if this works.

Related

How can I get information created in another application?

I want to send the user from application 1 to application 2 by pressing a button. In application 2, the user performs an action (for example, writes text), then he clicks the button and the text is sent to application 1.
I know that there is a certain "Getting a result from an activity", but I could not figure out how it works. I also found examples on YouTube where similar actions are performed, but inside one application. How can this be done using different applications?
You can launch an Activity using startActivityForResult(). The Activity that you launch does not necessarily need to be in the same application. When that Activity calls setResult() and then finish(), the information that was passed in setResult() will be returned to your original Activity in the method onActivityResult().
I'm sure that you can find some tutorials about how to use startActivityForResult().

At what moments can I get info from an intent?

I send an intent with information to start an activity. Now, instead of retrieving the info in the onCreate method, can I retrieve it after that moment? For example, when I click a button in the new activity?
Yes I think you can if you write it this way:
YourActivity.this.getIntent().getExtras();
getIntent().getExtras() yields the data in your current Activity.
If you want to pass this data to a new Activity you have to read them and write into a new Intent.
If the first activity is made as singleton then you can get the Intent data from the only Activity's instance.
Or you can make another singleton class just to hold that data for you.

Send String Data without Changing Activity(Intent)

I am trying to send the string from one Activity to Another WITHOUT changing the CURRENT ACTIVITY. This my code I used:
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("getFollowerNumberData", txt);
startActivity(intent);
Using this code bring me to the other activity or I do not want that. I just want to send this string without changing activity. I tried this one
Intent intent = new Intent();
But it is crashing my app. First of all it is possible to do so? If yes, how can I achieve that?
Edit: To be more clear, it works like follower and Following features in other apps or games. When you follow someone the number of the person you just followed goes up as well your Following number. Since my SignInPlayerProfile.Class(where the Following should go up to one too) is in another activity I was trying to get this information intent.putExtra("getFollowerNumberData, txt); from Main Activity and display that in the SignInPlayerPlayer by using text.setText(). But the Problem is by using intent = new Intent(this, AnotherActivity.class); it brings me to SignInPlayerProfile Activity which I do not like since
I am trying to send the string from one Activity to Another WITHOUT changing the CURRENT ACTIVITY(or simpler words WIHTOUT Going to the Other Activity).
Thanks for Help.
When an activity getting closed, according to it's life-cycle, it will be destroyed and no longer exists, when you start an activity it will be created and then it can get your data, so you can't send data to an destroyed activity which no longer exists
You can use Static Variables to communicate between activities (Classes), you can change value of a Static-Variable of an activity from another activity, but it's not a good option for data you need to be alive because Static Variables lives on Heap Memory and Heap will be freed if Android OS needs more Memory
Another way is to create a Message Handler in your first activity as a Static Variable and then send a Message to the Handler from second activity, see this example :
http://stacktips.com/tutorials/android/android-handler-example
I suggest you using SharedPreferences for saving your data in first activity and load from it on second activity
EDIT :
According to your edit, the "Number" you want to use in another Activity as "Following" or "Followers" is just needed when the second activity is visible, you should use sharedpreferences to save the "Number" and load from it when you need it. For example before text.settext() method you can load the number from sharedpreferences and then pass it to text.settext()
You should not save your data on the variables or classes and should save them on a file like a Database or SharedPreferences then you can load them every time you want
Furthermore you can search about Activity life-cycle and see how to use life-cycle events like OnStart to load your data
Firstly please consider startActivityForResult() ,we can send information from one activity to another and vice-versa. As mentioned consider using shared_preferences for local in memory storage. To truly accomplish this feat in a elegant way though, do consider using obervables(rxandroid). You publish observations in one activity or fragment ,
then subscribe in another activity or fragment. I did not mentioned event bus nor otto since rxjava/rxandroid surpasses it. They act a promise context management system. Also because observable in process dependent consider using broadcast receivers, to broadcast events through out your application and external if so desired.

Android how to pass pointer on List and receive it back

I'm developing a simple Application. It has 3 buttons (start record, stop record, send info to the server). I want to record my voyage on the map, and I implemented it, but it works only when Activity is onResume(), so now I implementing sticky service, which will do exactly the same, but in the background. So service will fill ArrayList<MapPoint>, and then I have to receive it back somehow to my activity. It is not necessary to pass ArrayList to Service, but I have to receive it back to send it to the server in onDestroy() method. Please, help.
I thinks you can put the value in shared preferences and then pull it back in the activity

Prevent duplicate activities

So, I'm both new to Java and to creating android apps, but not new to programming. I've read through most of the developer.android.com site, but I haven't been able to find this:
I want to make sure that a certain activity isn't running more than once at the same time. So we have a task somewhat like this:
Activity A) a TabActivity, which launches
Activity B) a ListView that, on-click, opens up
Activity C) which is the interface for a mediaplayer object
Right now, whenever somebody presses the back-button whilst in C (Which is a likely thing, because they're going to listen to a streaming 1-hour long mp3) and then presses another list item, instead of returning to C, C is opened a second time, and two streams are playing. Of course, I'd want only one instance of C running, and I want a second click on the list item to bring C back to the front. This could also be useful for notification intents.
I've been messing around with the flags (especially FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_REORDER_TO_FRONT), but no success so far.
If someone could help me out here I could focus on my next challenge - making it a real feed reader :P
Thanks in advance,
You need to flag your actvity as either "singleTask" or "singleInstance" in your manifest. I can't remember the exact differences between the 2, but either should do what you want. SingleInstance just does something different with the stack.
Example :
<activity android:name="MainActivity" android:launchMode="singleInstance"></activity>
You can handle new calls to startActivity() from the same activity instance with onNewIntent()
I've got it!
For those reading this question and wanting to know the summary: I mistakenly thought more then one activity was running, but it appeared more MediaPlayer instances where running. I made my mediaplayer a class member and am now controlling it from the onStart() event.
I am using SharedPreferences to check if the stream needs to reset and change source, or continue running and just show the interface.
Thanks for all your reactions. Really helped me out.
Just Edit the package names in .xml
EX: com.org.MainActivity
change to
.org.MainActivity
It works for me.....yup

Categories

Resources