0

I have a class that implements 'Serializable' but i am unable to get the context of my main activity in it. Here is my code for this class:

public class Game implements Serializable{
private String name;
private ColorTheme theme;
private int distance = 0;
private int score = 0;
boolean boom = false;
protected Context context;

public Game(MainActivity context){
    this.context = context.getApplicationContext();
}

After this i thought i get the context and then what i want is that when the score is greater than 1 then the app should closed.

score = (i+1);
if (score >1)
{

}

I want to close my game inside this condition but it is not giving me any option to finish the context. Please help me where i am wrong. I think the context is not successfully implemented in this Serializable class. Help needed

Arslan Ali
  • 371
  • 5
  • 20
  • How do you want to close your game? From the **Game** POJO? – Todor Kostov Sep 21 '16 at 08:48
  • I want to close when the score is greater than 1 – Arslan Ali Sep 21 '16 at 08:51
  • Ok, but WHO is going to do that? The **Game** class? – Todor Kostov Sep 21 '16 at 08:51
  • Yes, this score condition is in the Game Class that implements Serializable but i am not getting the context of my main activity in this game class – Arslan Ali Sep 21 '16 at 08:52
  • Ok, I the is such a BAD BAD design! POJOs classes like **Game** should not be responsible for such things! That MUST be done from your Activity class, or better from your Presenter class if you are using MVP design pattern! – Todor Kostov Sep 21 '16 at 08:55
  • can u plz help me how to do this from my activity class? like first i had to get the score in my main acitivity and then put a condition and then finish – Arslan Ali Sep 21 '16 at 08:56
  • Just make a Game object in your Activity and every time you increase / decrease the score property just check if your condition is satisfied. If YES - finish the Activity. If you do it in your activity you will not even need to use Context at all. – Todor Kostov Sep 21 '16 at 08:59
  • You need to redirect user from this activity to another for setting clear top in intent to close all activities and there you have to check by bundle value and close activity so entire app will be closed – Vickyexpert Sep 21 '16 at 09:00
  • @TodorKostov Ok thankyou so much – Arslan Ali Sep 21 '16 at 09:01
  • @Vickyexpert i got you too thanks :) – Arslan Ali Sep 21 '16 at 09:02

2 Answers2

0

FYI

Closing an application inside the Game class, is not a good approach, it's better to do it inside the Activity itselfs. A good solution for this could be creating a method inside the Game class like the following:

public boolean shouldCloseApplication(){
  return this.score > 1;
}

and in the Activity, use the following:

if(game.shouldCloseApplication()){
  //close the application as mentioned below
  this.finishAffinity();
}

Said that, if you wanna close the application, you simply need to call

this.finishAffinity();

refer to this SO post.

So, instead of passing "MainActivity", use directly the "Context" passing it with

Game game = new Game(MainActivity.this);

and receiving with

public Game(Context context){
  this.context = context;
}

and use it with

if(score > 1)
{
  context.finishAffinity();
}

Not suggested, but works

if this doesn't work, use

context.getActivity().finish();
System.exit(0);

Hope this helps

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • Ok let me try this one SIr – Arslan Ali Sep 21 '16 at 08:51
  • As mentioned in the comments bellow the original question - this is a BAD design! – Todor Kostov Sep 21 '16 at 09:05
  • @TodorKostov what do you mean? which part is a bad design? – Pier Giorgio Misley Sep 21 '16 at 09:09
  • @PierGiorgioMisley check the discussion bellow the original question. – Todor Kostov Sep 21 '16 at 09:10
  • @TodorKostov you mean the System.exit(0)? – Pier Giorgio Misley Sep 21 '16 at 09:10
  • @PierGiorgioMisley no, I mean that **Game** should not be responsible for finishing and killing activities / the app etc. Thus, having the Context attribute in the **Game** POJO is a bad design. – Todor Kostov Sep 21 '16 at 09:12
  • @TodorKostov ok, I think this exactly same as you, but this is not about my answer, if he wants to do it and he is asking about how to do this thing, I simply told him how to solve this problem, because this is what an answer should do. btw I will add the tip about not suggested solution – Pier Giorgio Misley Sep 21 '16 at 09:14
  • @TodorKostov done, I've added a suggested solution for this, but my opinion is still that if he ask for something, he might have his reasons – Pier Giorgio Misley Sep 21 '16 at 09:19
  • @PierGiorgioMisley now your FYI edit is way better! Simply giving the OP an answer for his / her problem is not enough. He / She will think that THIS is the right way of solving the problem without even asking himself if there is a better way. And in future when working with other more experienced developers, the OP will have huge problems. And his reasons for approaching this problem in this way are wrong. – Todor Kostov Sep 21 '16 at 09:22
  • @TodorKostov Never thought at this point of view. Thanks a lot! I will keep it in mind for my future answers – Pier Giorgio Misley Sep 21 '16 at 09:23
-1

try this

public Game(MainActivity context){
this.context = context;
}


score = (i+1);
if (score >1){

PackageManager pm = context.getPackageManager();
                        Intent i = new Intent("android.intent.action.MAIN");
                        i.addCategory("android.intent.category.HOME");
                        List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
                        if (lst != null) {
                            for (ResolveInfo resolveInfo : lst) {
                                try {
                                    ApplicationInfo ai = pm.getApplicationInfo(
                                                    resolveInfo.activityInfo.packageName,0);
                                    if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                                        Intent goHome = new Intent();
                                        goHome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        goHome.setClassName(
                                                resolveInfo.activityInfo.packageName,
                                                resolveInfo.activityInfo.name);
                                        context.startActivity(goHome);
                                        int pid = android.os.Process.myPid();
                                        android.os.Process.killProcess(pid);
                                        break;
                                    }
                                } catch (NameNotFoundException e) {
                                    logMessage(LOG_TYPE_ERROR, "AppConstant-exitFromApp-kill", e.getMessage());
                                }
                            }
                        }

}
sajan
  • 389
  • 5
  • 11
  • the finish() keyword only closes the current activity, it doesn't exit the application if he has more than one activity open in the stack – Pier Giorgio Misley Sep 21 '16 at 08:52
  • Please, add some explanation to your answer like what exactly the code is doing and why! Also please reconsider your design! This is such a bad approach! – Todor Kostov Sep 21 '16 at 09:06