Stembolt
Stembolt

Reputation: 37

Second Activity Not Launching

I am working through the BigNerdRanch book in order to increase Android proficiency. After days and days of running code and having my screen dim on me I decided to add a command to wake/un-dim the screen so I didn't have to continually reach over and tap the extremely dim power-saving screen in order to check on my program.

This led to a whole slew of Google, StackOverflow, etc searches the most relevant of which I will attach at the bottom of this post. The problem I face now is back to a basic one:

My second activity is not launching and I know that because the break points I have placed at arbitrary points within the code are catching nothing. I have no idea why.

Here is the relevant segment of my launcher activity code:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);

    //screen refresher start
    android.provider.Settings.System.putInt(getApplicationContext().getContentResolver(),
            Settings.System.SCREEN_BRIGHTNESS, 255);

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = 255; // 100 / 100f;
    getWindow().setAttributes(lp);

    Intent k = new Intent(SingleFragmentActivity.this, RefreshScreen.class);
    startActivity(k);
    //screen refresher end

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragment_container);
    if (fragment == null){
        fragment = createFragment();
        fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
    }
}

Here is the second activity's code

public class RefreshScreen extends Activity {

int x = 5;

protected void OnCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    this.finish();
    return;
    }
}

I put break points a the following locations:

  1. Always works.

    startActivity(k);

  2. Never arrives.

    int x = 5;

  3. Never arrives.

    super.onCreate(savedInstanceState);

  4. Never arrives.

    return;

If for some reason what I was attempting to do would help you, here is the relevant link.

Changing screen brightness programmatically (as with the power widget)

Upvotes: 0

Views: 56

Answers (3)

Stembolt
Stembolt

Reputation: 37

While the solutions posted worked for my main problem, "Using OnCreate instead of onCreate" I wanted to follow up and say that I solved the initial problem I was working on. It turns out that I was barking up the wrong tree looking to programatically adjust the brightness on each run of the program, instead I discovered that a WakeLock would equally solve my problem as it keeps the screen on max brightness for those times when you are programming plugged-in-charging and don't care about battery life.

Here is the code I used in the Class I named BrightScreen.java, I threw in a caveat that if the Battery Level was under 50% it would not WakeLock again until the phone was charged above 50% for those times when you are working on a low amperage USB port.

//screen brightness refresh start
    PowerManager pm =
            (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl =
            pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "WakeAct");

    //Retreive the current charge status from the BatteryManager sticky Intent
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter);

    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    float batteryPercent = level / (float)scale;

    if (batteryPercent > 50){
        wl.acquire();
    } else {
        if (wl.isHeld()){
            wl.release();
        }
    }
    //screen brightness refresh end

I hope this helps someone out there. The two key components are wl.acquire() and wl.release().

Upvotes: 1

Arnab Jain
Arnab Jain

Reputation: 247

Change your second activity like this :

public class RefreshScreen extends Activity { 
    int x = 5; 
    public void onCreate(Bundle savedInstanceState){ 
        super.onCreate(savedInstanceState);
        this.finish();
    }
}

Explanation : 1. The protected modifier causes the acticity to not open when called. 2. OnCreate() is wrong. It should be onCreate() 3. return(); after finish(); is usless.

Upvotes: 1

FranMowinckel
FranMowinckel

Reputation: 4343

Check the method name in the second activity, it should start with lower case:

@Override
protected void onCreate(Bundle savedInstanceState)

Upvotes: 1

Related Questions