StartActivityForResult() from a own class

Put your problem here if it does not fit any of the other categories.

StartActivityForResult() from a own class

Postby Razer6 » Thu Jun 24, 2010 7:04 am

Hi Guys,

For a SharedPreference I created my own Preference:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CustomPreferenceControl extends Preference
  2. {
  3. }
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


This class contains an OnClickListener which should start another Activity (a filebrowser). The Activity should be started with startActivityForResult() to get the browsed path.

But how can I do that because CustomPreferenceControl isn't derived from Activity.

Thanks in advance!
Regards Robert
Razer6
Freshman
Freshman
 
Posts: 7
Joined: Thu Jun 24, 2010 6:56 am

Top

Re: StartActivityForResult() from a own class

Postby padde » Thu Jun 24, 2010 4:26 pm

Pass the context of the activity to that class and use that context to
start the subactivity.


Your Custom class
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CustomPreferenceControl extends Preference {
  2.     private Context mCtx;
  3.  
  4.     public CustomPreferenceControl(Context c) {
  5.         this.mCtx = c;
  6.     }
  7.  
  8.     public startActivity() {
  9.         ...
  10.         mCtx.startActivityFromResult(....);
  11.         ...
  12.     }
  13. }
  14.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Your activity where you create an object of your custom class
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyActivity extends Activity {
  2.     ....
  3.     CustomPreferenceControl cpc = new CustomPreferenceControl(this);
  4.     cpc.startActivity();
  5.     ...
  6. }
  7.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Something like that...
Another possibility would be to make a static Context variable in your Activity class and use it
in your other classes.

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: StartActivityForResult() from a own class

Postby Razer6 » Thu Jun 24, 2010 4:44 pm

Hi padde,

Thanks for response.
I know this method but it's not optimal.

How can I catch the result of the called Subactivity? But how to implement onActivityResult() in CustomPreferenceControl()? This is a delegated mathod of activity?

But there is a bigger problem. My PreferenceActivity is based on a XML File:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4.  
  5. <ListPreference>
  6. ....
  7. ></ListPreference>
  8.  
  9. <razer.preferencetest.CustomPreferenceControl >
  10. ....
  11. </razer.preferencetest.CustomPreferenceControl
  12.  
  13. </PreferenceScreen>
  14.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


So I don't create the CustomPreference not by code and can't pass the Activity.

I hope someone can help me
Regards Robert
Razer6
Freshman
Freshman
 
Posts: 7
Joined: Thu Jun 24, 2010 6:56 am

Re: StartActivityForResult() from a own class

Postby padde » Thu Jun 24, 2010 5:59 pm

Maybe Preference.OnPreferenceChangeListener is a way. You listen to preferences changes in your activity
and call the subactivity from there if you have to. The result of the subactivity lands in your activity in either way..
and there is no method i am aware of to delegeate it. Maybe rethinking your hole idea of handling your preferences
etc. and redesign your architecture is a good idea.. because for me its sounds like you have hit a dead end there.

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: StartActivityForResult() from a own class

Postby Razer6 » Thu Jun 24, 2010 6:30 pm

Thanks for you reply. I could it see, that this is not the perfect way.

Let me explain my Problem. I need a custom preference for a "path" setting. For this reason I wrote a browser activity. When I click on my preference, i want to open this preference (path is controlled by intent). After browsing the path and closing the Activity, I want to get the new path.

Robert
Razer6
Freshman
Freshman
 
Posts: 7
Joined: Thu Jun 24, 2010 6:56 am

Re: StartActivityForResult() from a own class

Postby padde » Thu Jun 24, 2010 6:58 pm

I think it didnt get it right.. so you have your activity X and from a menu or something like that you run a
preference activity to change a path setting. When you return from your preference activity you want activity X
to work/know the new path setting.
Is that right?

I plan to look into the problem and hopefully come up with a solution but i have to understand the problem first in all
its colors :)

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Re: StartActivityForResult() from a own class

Postby Razer6 » Thu Jun 24, 2010 7:58 pm

Hi,

I have a PreferenceActivity which gets its elements from a preference.xml (also my CustomPreferenceControl). So filling the setting screen is done automatically.

Clicking on my control should open the browser at the current path. After selecting the new, it should be stored in the preference settings.

Thank you for your great help!
Robert
Razer6
Freshman
Freshman
 
Posts: 7
Joined: Thu Jun 24, 2010 6:56 am

Re: StartActivityForResult() from a own class

Postby padde » Thu Jun 24, 2010 8:23 pm

Ah so your CustomPreferenceControl is your own path selector widget thingy for your preference screen?!
I hope i got it right now *g*
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: StartActivityForResult() from a own class

Postby Razer6 » Thu Jun 24, 2010 8:33 pm

padde wrote:Ah so your CustomPreferenceControl is your own path selector widget thingy for your preference screen?!
I hope i got it right now *g*


Yes. An a click to this should open my filebrowser.
Razer6
Freshman
Freshman
 
Posts: 7
Joined: Thu Jun 24, 2010 6:56 am

Re: StartActivityForResult() from a own class

Postby padde » Thu Jun 24, 2010 8:46 pm

Okay.. i looked at the Preference class and you can get the context from the preference method
"getContext()" also all constructors of that class are needing the context. So try to implement
those constructors and/or try using getContext to get the context to call you subactivity.

Here is the Android Dev Site about Preference Class:
http://developer.android.com/reference/ ... rence.html

And this is the source for the CheckBoxPreference and the other system preference elements:
http://www.google.com/codesearch/p?hl=e ... erence&d=4

The rest should just be a matter of code customising to fit your needs.

Good luck :)
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: StartActivityForResult() from a own class

Postby Razer6 » Thu Jun 24, 2010 9:32 pm

The problem is, I can't call startActivityForResult() with this context. So I won't get the browsed path.
Razer6
Freshman
Freshman
 
Posts: 7
Joined: Thu Jun 24, 2010 6:56 am

Re: StartActivityForResult() from a own class

Postby padde » Thu Jun 24, 2010 10:01 pm

Hmm is see.. you just can use startActivity. So the problem is how does your preference activity get the results
from your filebrowser. First thing that comes to my mind is .. why not putting the filebrowser into your CustomPreferenceControl?! Because right now it seems to be just a bridge thats calling another activity that is
actually handling the path.. what is causing your problem.

A few other ideas:
- Let the filebrowser save the selected path to the preference file (so u dont need the result of the activty).
(here you would then know when and what changed with the listener i mentioned before)

- On click of your CustomPreferenceControl save the curent state to the preference file.. call the filebrowser (without result).. close the preference activty and callback the preference activity from your filebrowser with an intent that carry the selected path.

- Provide a static method in your preference activity that calls the filebrowser (with result). Use that function insider your CustomPreferenceControl class. The result will be provided in your onActivityResult method (inside
your preference activity). From there you can pass the result to your CustomPreferenceControl class via a
setResult method or something like that.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: StartActivityForResult() from a own class

Postby Razer6 » Thu Jun 24, 2010 10:17 pm

padde wrote:A few other ideas:
- Let the filebrowser save the selected path to the preference file (so u dont need the result of the activty).
(here you would then know when and what changed with the listener i mentioned before)

- On click of your CustomPreferenceControl save the curent state to the preference file.. call the filebrowser (without result).. close the preference activty and callback the preference activity from your filebrowser with an intent that carry the selected path.

- Provide a static method in your preference activity that calls the filebrowser (with result). Use that function insider your CustomPreferenceControl class. The result will be provided in your onActivityResult method (inside
your preference activity). From there you can pass the result to your CustomPreferenceControl class via a
setResult method or something like that.


Thank you for you help. T´he first solution seems to be the best because there there is no code in the PreferenceActivity - so it's a quite good generic solution which helps to reuse the code.

Thank you, I will implement that tomorrow.
Best regards from Austria
Robert
Razer6
Freshman
Freshman
 
Posts: 7
Joined: Thu Jun 24, 2010 6:56 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 18 guests