Intents.. am I missing something?

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Intents.. am I missing something?

Postby atmofan32 » Fri Jun 25, 2010 7:55 pm

First of all..hello everyone!

I had a quick question that I hope one of you can seriously help me out with.

I feel like I've exhausted every option and using intents gets me 90% of what I want.

Here is the problem

Basically all I want to do is send a few variables to another Activity WITHOUT starting the new Activity!!!

I hope I am being clear, and if I'm not please let me know and I will try to explain this further.

I appreciate any help / feedback :D
atmofan32
Freshman
Freshman
 
Posts: 5
Joined: Fri Jun 25, 2010 7:37 pm

Top

Re: Intents.. am I missing something?

Postby vengeance_mj » Sun Jun 27, 2010 3:11 am

if u do use Intents, in order that the other activity does receive them u need to use startActivity(intent) or startActivityForResult(int,status_code) so well you may not set the content view for the activity.
Well, if you do not need to start the activity, why are you creating it in the 1st place? try to post the problem scenario, hopefully we can help u
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Re: Intents.. am I missing something?

Postby atmofan32 » Sun Jun 27, 2010 6:13 pm

Thanks veangance_mj for the reply!

Ok so basically the issue is, I am using the tab widget feature. Currently I only have two tabs, which have separate Activities.

Activity A asks for user input, and then outputs the value given in a textview after hitting submit.

Actvity B is going to take the user input from Activity A, and make its own calculations based on the user defined variable.

The problem is, I tried using public static variables (which is something I wanted to avoid) and do it that way. Which worked in a way, but if I were to switch back to Activity A and change the variable again, Activity B would not update the modified output.

My solution was to use intents, which I picked up off some online documentation and it works perfect EXCEPT it switches over to Activity B (the second tab) after I hit submit automatically, which it should. I'm just looking to see if there is a way to use the intent without switching over to the new activity / view.

Essentially, is there something else instead of startActivtiy() that will allow me to pass the user defined variable without having to automatically start the new Activity.

I really hope this isn't too confusing and if I'm being an idiot please say so haha, I'm new to all this and I've been really having a tough time trying to find the correct solution.

Thanks for taking the time to read this, and any help is much appreciated!
atmofan32
Freshman
Freshman
 
Posts: 5
Joined: Fri Jun 25, 2010 7:37 pm

Re: Intents.. am I missing something?

Postby vengeance_mj » Sun Jun 27, 2010 9:28 pm

Well as I said before if you are only using a dummy activity to do some processing why use Activity B? do the processing in Activity A. If Activity B needs to display its results on the user input data then use Activity B else do not
As far as the thing goes, all intents are fired by using startActivity() no other method. So well, you can just receive all the inputs in activity B and then probably have an intent which fires back to Activity A.

As far as making mistakes, well even I have made some rookie mistakes so it happens. you gotta make mistakes to learn
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Re: Intents.. am I missing something?

Postby atmofan32 » Mon Jun 28, 2010 3:17 pm

Thanks for the responses so far vengeance_mj..your points are valid for sure..but I think I made the mistake of leaving a portion of my program out.

On the second Activity /view .. I want to also have user input that will be using the input from Activity A to form the calculations. This changes my problem a bit. Otherwise you would be correct in saying that I should do all of my calculations in one Activity if the purpose of the second was to only output the calculations.

Any thoughts? :?
atmofan32
Freshman
Freshman
 
Posts: 5
Joined: Fri Jun 25, 2010 7:37 pm

Re: Intents.. am I missing something?

Postby vengeance_mj » Mon Jun 28, 2010 4:30 pm

Ok well since you do need user input from Activity B why don't you want to switch to the other activity . The one logical work flow would be to accept input in Activity A and if he clicks a button say Submit pass them all as Intent extras and you can probably output the values in a textview in Activity B or maybe use an onFocusListener to show the output dynamically.
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Top

Re: Intents.. am I missing something?

Postby atmofan32 » Mon Jun 28, 2010 5:24 pm

It's not that I don't want them to switch to the new activity..I want them to use the tabs to cycle through the Activities instead.

So for example:

Activity A / Tab 1

User input : 2

Button (stores input from user)

Now when the user clicks the button, I want all the values they entered sent to the next activity without it automatically starting.

---------------------------------------------
Activity B / Tab 2

User input : 5

Runs calculation using the first activity input and second activity input, to output the current calculation.


So I almost want all of the activities to share variables transparently, so when the user is ready to go to the second tab / activity, the values are there and ready to be calculated with their input for Activity B.
atmofan32
Freshman
Freshman
 
Posts: 5
Joined: Fri Jun 25, 2010 7:37 pm

Re: Intents.. am I missing something?

Postby padde » Mon Jun 28, 2010 5:37 pm

How about a application class and store your public static values there.. should work.
Another way would be a preference file. Sending an intant to an activity without starting the
activity isnt possible i think.. i guess you tried all flags like single top and that stuff.
Or try calling activity B from A with startActivityForResult and in onCreate of B call back to A asap.
There is something in the docu that this would prevent activity B from showing.

startActivityForResult docu:
As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial
onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be
displayed until a result is returned back from the started activity. This is to avoid visible flickering
when redirecting to another activity.


Maybe you can use this special case for your advantage.. but i guess the Application Class approach or
going with a preference file is cleaner.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Intents.. am I missing something?

Postby vengeance_mj » Mon Jun 28, 2010 9:27 pm

@atmofan32 as padde has stated, a preferences file will be a good approach or u can use startActivitryForResult(intent) and in the onCreate method just call setResult(intent). I might try to implement the thing which you want to do maybe tonight or tomorrow, so let me know if I need to know some other details than the ones you posted
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Re: Intents.. am I missing something?

Postby atmofan32 » Mon Jun 28, 2010 11:22 pm

I appreciate all the feedback both vengeance_mj and padde..I will look into your suggestions to see if they better suit my needs!

Vengeance, thanks for taking the time to implement this..I REALLY would love to know how to work something like this. I appreciate the help a lot guys.

Love the hospitality! :P
atmofan32
Freshman
Freshman
 
Posts: 5
Joined: Fri Jun 25, 2010 7:37 pm

Re: Intents.. am I missing something?

Postby Brocade » Sat Jul 03, 2010 3:15 pm

could just use a bundle
Brocade
Freshman
Freshman
 
Posts: 4
Joined: Sat Jul 03, 2010 2:42 pm

Re: Intents.. am I missing something?

Postby limteckchai » Wed Jul 21, 2010 8:46 am

I got an idea, what u can do is pass it over in a bundle.

Intent.putExtra("Number",<Int>);
Intent.putExtra("Back",<boolean>);

That way, u move the int over.
As for the Back, what is meant to be done is that you can actually if-else and redirect it back to the tab you want it to be.
limteckchai
Experienced Developer
Experienced Developer
 
Posts: 92
Joined: Mon Jun 28, 2010 3:33 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests