0

Excuse me if this comes as a foolish question but I am a bit confused. I am creating a new application in which I am using a Tab Activity. However in that activity, I need to refer to other classes which require an activity as their parameters. For example, I have created a permission class to check for permissions when I try to access the camera and other things that require run time permissions.

But in order to use the requestPermissions function, it requires an activity class in order to do so. So in this case, is it OK in this case to pass my Tab Activity to my Permission class or should I just request the permissions in my Tab Activity?

Similarly, I also have a separate class to do my AsyncTask functions. In the onPostExecute of the AsyncTask, I want to display a dialog using my TabActivity context. From my research, I have concluded that for this I should make a static function in TabActivity which would handle that for me. Is this the correct way to go about it?

Thanks!

moe_nyc
  • 307
  • 1
  • 4
  • 18
  • 1
    I did not understand the first question. For the second question, you should put a callback into your onPostExecute that leads back to the activity, and you should build your dialog in there. https://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui That's better than using a static method. – Mohamed Hamdaoui Nov 22 '17 at 18:48
  • @MohamedHamdaoui if I want to handle run time permissions in another class, do I have to pass that class an instance of my Activity if the permission class is not extending Activity. – moe_nyc Nov 22 '17 at 18:53
  • 1
    I see what you mean. Whenever I am faced with this, I try to handle the permission in the Activity itself, there must have been a point where the Activity "starts" that class, and in there, I would register a listener for that class to tell the activity when to ask for permission, then once the permission result is received in the Activity, it would call another method in that class to continue working. It's better to have an instance of that class in the activity than the other way around – Mohamed Hamdaoui Nov 22 '17 at 18:57
  • @MohamedHamdaoui Thank you very much brother! You have been very very helpful! – moe_nyc Nov 22 '17 at 19:10
  • @MohamedHamdaoui Once more question. Now in my tab Activity, I have 3 fragments in one of which I might need to ask for permissions which can be accessed in the Tab Activity. Should I use an interface in this case to communicate between the fragment and activity? or should I do this ((YourActivityClassName)getActivity()).yourPublicMethod(); – moe_nyc Nov 22 '17 at 19:20
  • This is an architectural decision in my opinion. I always like to have the activity take care of its business, so I wouldn't use ((YourActivityClassName)getActivity()).yourPublicMethod(); but rather interfaces. But this is just a personal preference and I don't see a draw back for the other way – Mohamed Hamdaoui Nov 22 '17 at 21:29

0 Answers0