popBackStackImmediate() not working - android

#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View h = getActivity().findViewById(R.id.gohome);
h.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
new Thread(new Runnable() {
public void run() {
Log.w("tt", "tttt");
if(MainActivity.fm.getBackStackEntryCount() > 1) {
try {
MainActivity.fm.popBackStackImmediate("tiles", 0);
}
catch(Exception e) {
Log.w("tt", "tttt" + e);
}
}
//((MainActivity)arg0.getContext()).toggle();
}
}).start();
}
});
}
I have a home button on my app. When clicked, i wanted to return to the fragment names "tiles" which is the bottom most fragment. But this is not working ... plz help

Related

Unable to synchronize threads in Android Studio

I have a background class named "TCP client" that sets a boolean to true whenever a state change is detected in hardware. And runOnUIThread in main activity detects this and updates UI which are switch buttons, in order not to trigger the OncheckedChangeListener I set the listener to null, change state, then set the listener to its original one. But during runtime OncheckedChangeListener is still triggering. I guess this is a synchronization problem.
mainActivity.java
public class MainActivity extends AppCompatActivity {
public SwitchButton.OnCheckedChangeListener switchListener;
public com.suke.widget.SwitchButton switch;
public static boolean switchflag=false;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
switch = findViewById(R.id.lightSwitch);
runThread();
switchListener = new SwitchButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(SwitchButton view, boolean bChecked) {
if (bChecked) {
Log.d("button", "light is on");
connection.tcpClient.sendMessage("li1_1");
} else {
Log.d("Button", "light is off");
connection.tcpClient.sendMessage("li1_0");
}
}
};
switch.setOnCheckedChangeListener(switchListener);
}
private void runThread() {
i = 0;
new Thread() {
public void run() {
while (i++ < 1000) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
if(connection.tcpClient.getStatusFlag()) {
Log.d("main", "got Status");
updateUI();
connection.tcpClient.setStatusFlag(false);
}
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
public void updateUI() {
try {
switch.setOnCheckedChangeListener(null);
switch.setChecked(switchFlag);
switch.setOnCheckedChangeListener(switchListener);
}catch (NullPointerException e) {e.printStackTrace();}
}
I am not getting where to use the synchronized.
Put
runThread();
below
switch.setOnCheckedChangeListener(switchListener);
Also Replace
switch.setChecked(lightFlag);
With
switch.setChecked(switchFlag);

Mediaplayer Background Audio not work properly Using URL And Song Not Play Second Time

This is my second activity and when open this activity threw button activity take load and open after some time and some times UI Freeze and some time black screen open for some seconds
public class AartiActivity extends AppCompatActivity {
final MediaPlayer player1 = new MediaPlayer();
private Button btn_playarti, btn_pausearti, btn_stoparti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aarti);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
btn_playarti = (Button) findViewById(R.id.btn_playarti);
btn_pausearti = (Button) findViewById(R.id.btn_pausearti);
btn_stoparti = (Button) findViewById(R.id.btn_stoparti);
try {
player1.setDataSource(STRING_URL);
player1.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btn_playarti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
player.stop();
}
});
player1.start();
}
});
btn_pausearti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (player1.isPlaying()) {
player1.pause();
}
}
});
btn_stoparti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.stop();
}
});
}
#Override
protected void onStop() {
super.onStop();
player1.stop();
}
}

Activity.runOnUiThread(Runnable action) only updates view once

In my fragment, I have an AlertDialog and a Bluetooth connection manager. I want to update the AlertDialog with the new states of the Bluetooth connection process, so I used the runOnUiThread(...) method:
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
interactor = new BluetoothInteractor(getActivity(), new OnBluetoothStatusChangedListener() {
#Override
public void OnConnectionStopped() {
alertDialog.setMessage("Disconnected.");
}
#Override
public void OnConnectionStopping() {
alertDialog.setMessage("Stopping connection...");
}
#Override
public void OnConnectionStarting() {
alertDialog.setMessage("Connecting to device...");
}
#Override
public void OnConnectionStarted() {
alertDialog.setMessage("Streaming data...");
}
});
}
});
The first time I update the AlertDialog message (OnConnectionStarting event) everything works fine, but the second time I got android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
What could be happening here?
Replace with
interactor = new BluetoothInteractor(getActivity(), new OnBluetoothStatusChangedListener() {
#Override
public void OnConnectionStopped() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Disconnected.");
}
});
}
#Override
public void OnConnectionStopping() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Stopping connection...");
}
});
}
#Override
public void OnConnectionStarting() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Connecting to device...");
}
});
}
#Override
public void OnConnectionStarted() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Streaming data...");
}
});
}
});

How to perform click continuously while something is enable

I'm beginner... I need to run some code after 10 seconds continuously while my button is enable.
and when I clicked on it and it turned to disable-state, timer get stop.
I use below code but it run just once when I click on timerbutton every time again...
I think I have to use threed and I think I used it too! But I did not get to my goal.
private View.OnClickListener ontimerclicked = new View.OnClickListener() {
#Override
public void onClick(View view) {
Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (endistimer==false) {
endistimer=true;
varbtnimgslidtimer.setBackgroundColor(Color.parseColor("#E91E63"));
varbtnimgnext.performClick();
intdelay=10000;
}
else
{
endistimer=false;
varbtnimgslidtimer.setBackgroundColor(Color.parseColor("#dddddd"));
intdelay=0;
}
}
}, intdelay);
}
};
I finaly solved it with below code.I put it for anothers:
private View.OnClickListener ontimerclicked = new View.OnClickListener() {
#Override
public void onClick(View view) {
mHandler = new Handler();
if (endistimer==false) {
endistimer=true;
varbtnimgslidtimer.setBackgroundColor(Color.parseColor("#E91E63"));
}
else
{
endistimer=false;
varbtnimgslidtimer.setBackgroundColor(Color.parseColor("#dddddd"));
intdelay=5000;
cntsec=0;
varbtnplusfive.setText(arrsec[0] +" Sec");
}
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
while (endistimer) {
try {
Thread.sleep(intdelay);
mHandler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// your codes
// you can set continue_or_stop to false, for stop
}
});
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(slideword.this,e.toString(),Toast.LENGTH_SHORT);
}
}
}
}).start();
}
};

Camview request Focus on touch

I am using this library CamView from LivotovLabs to scan barcodes. It works as expected however it does not focus for specific phones so I want to implement a focus on touch and my solution is as below. However its not working,the phone does not focus on the barcode.
camera = (ScannerLiveView) findViewById(R.id.camview);
camera.setFocusable(true);
camera.setFocusableInTouchMode(true);
camera.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
camera.requestFocus();
return true;
}
});
camera.setScannerViewEventListener(new ScannerLiveView.ScannerViewEventListener() {
#Override
public void onScannerStarted(ScannerLiveView scanner) {
}
#Override
public void onScannerStopped(ScannerLiveView scanner) {
}
#Override
public void onScannerError(Throwable err) {
Toast.makeText(ACME_Scanner.this, "Scanner Error: " + err.getMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeScanned(String data) {
Toast.makeText(ACME_Scanner.this, "Barcode No: "+data, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btnFlash).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleFlash();
}
});
findViewById(R.id.btnSkip).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
protected void onResume()
{
super.onResume();
ZXDecoder decoder = new ZXDecoder();
decoder.setScanAreaPercent(0.5);
camera.setDecoder(decoder);
camera.startScanner();
}
#Override
protected void onPause()
{
camera.stopScanner();
super.onPause();
}
public void toggleFlash()
{
try {
flashStatus = !flashStatus;
camera.getCamera().getController().switchFlashlight(flashStatus);
}
catch (NullPointerException e){
e.printStackTrace();
}
}

Categories

Resources