Button finish activity after 2 press is WRONG: Android - java

I want the button to finish the activity. Why do I have to press it TWICE for it to finish, I do not want this. Note, I have to press the button twice before the activity ends, which I do not want.
MainActivity Class:
private Runnable updateOkay= new Runnable() {
public void run() {
if (true) {
Intent i = new Intent(this, WorkTimerNotification.class);
startActivity(i); }
WorkTimerNotification class:
public Button confirmButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
confirmButton = (Button) findViewById(R.id.confirmOK_button);
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}

Solved by setting
android:launchMode = "singleInstance"
in AndroidManifest.xml file.

Related

How to finish other activity when back button is pressed in splash activity?

So im working on slideshow-like project, but my problem is, when im at the middle of slideshow, I want to go back at my main activity where my slideshow will start using back button on my smartphone. What happen is, it will go back as soon as I pressed back button, but the other slideshows will carry on after 3 seconds which I set in my handler.postDelayed(r1, 3000);. Please help. Thank you
public class Dad1 extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dad1);
Runnable r1 = new Runnable() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), Dad2.class);
startActivity(i);
finish();
}
};
Handler handler = new Handler();
handler.postDelayed(r1, 3000);
}
}
What you can do is keep the Handler object at the Activity level. When back button is pressed then just cancel the Handler callbacks. So in that case your Handler's run method will not be called.
Please find the sample code below:
public class Dad1 extends AppCompatActivity {
private Handler handler;
private Runnable myRunnable;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initRunnable();
handler.postDelayed(myRunnable,3000);
}
private void initRunnable() {
myRunnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), Dad2.class);
startActivity(i);
finish();
}
};
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(handler!=null){
handler.removeCallbacks(myRunnable);
}
}
}

How to start an Activity task using Threads?

I want to start the Main2Activity's task(Loading the WebView) when button is pressed but not to show the Main2Activity's Screen until onPageFinished() is called. i had some ideas to get this by Intents however it seems doesn't work.
Here my code :
MainActivity.java :
public class MainActivity extends AppCompatActivity {
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Runnable runnable = new Runnable() {
#Override
public void run() {
handler.sendEmptyMessage(0);
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
);
}
}
Main2Activity.java :
public class Main2Activity extends AppCompatActivity {
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final WebView webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webView, url);
intent.putExtra("DONE",1);
// Toast.makeText(getApplicationContext(), "Done!",
//Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("file:///android_asset/2.2.html");
}
}
is this possible using Intents or i have to do this with some other techniques?
How can i Achieve this goal ?
Thanks All.
I think you should directly go to Main2Activity on button click, without calling any thread in MainActivity.
Then in Main2Activity show an image or something until onPageFinished() is called.

How to start needed Activity, when I saved data in file?

When app starts at first, start Activity with two button: Create File and Settings. Then if I click on button Create File - start Activity where I write text and save in file.
If we have error when file saved - start activity with two button. Else start another Activity with three buttons: Look file, Edit File, Settings.
How organize correct transitions between this Activity and How start activity with three buttons if file already saved?
public class MainFirstActivity extends AppCompatActivity {
private Button createFile;
private Button settings;
private boolean start = true;
private static final String MY_SETTINGS = "my_settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_first);
createFile = (Button) findViewById(R.id.create_file);
settings = (Button) findViewById(R.id.settings);
createFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainFirstActivity.this, CreateFileActivity.class);
startActivity(intent);
// MainFirstActivity.this.finish();
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainFirstActivity.this, SettingActivity.class);
startActivity(intent);
}
});
}
#Override
protected void onResume() {
super.onResume();
File f = new File(CreateFileActivity.FILE_NAME);
if(!f.exists() && !f.isDirectory() && start) {
Intent intent = new Intent(MainFirstActivity.this, MainWithFileActivity.class);
startActivity(intent);
//start=false;
MainFirstActivity.this.finish();
Log.e("err", "intent");
}
}
}
MainWithFileActivity
public class MainWithFileActivity extends AppCompatActivity {
public static final String MY_SETTINGS = "MY_SETTINGS";
private Button lookFile;
private Button editFile;
private Button settings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_if_first);
lookFile = (Button) findViewById(R.id.look_file);
settings = (Button) findViewById(R.id.settings);
editFile = (Button) findViewById(R.id.edit_file);
lookFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this, LookFileActivity.class);
startActivity(intent);
}
});
editFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this,CreateFileActivity.class);
startActivity(intent);
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this, SettingActivity.class);
startActivity(intent);
}
});
}
}
Simply check if the file exists. If it does not, hide the lookFile button
Add this in your MainWithFileActivity onCreate method
File f = new File(CreateFileActivity.FILE_NAME);
if(!f.exists() && !f.isDirectory()) {
lookFile.setVisibility(View.GONE);
}

Adding second onClick method in Android Studio?

I've just started to learn Java and I've been stumped on adding the code for a second button in an activity. I apologize for my (possible dumb question) and any wrong terminology.
Here is the MainActivity Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, GoActivity.class));
}
});
}
}
How can I add to this code for btnEscape to go to EscapeActivity?
You just have to do exacly the same thing you do with btnGo - this is find your button by id, and then set clickListener to it. It could looks like that:
Button btnEscape = (Button) findViewById(R.id.btnEscape);
btnEscape.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, EscapeActivity.class));
}
});
The optimised way to do that is implement your class with View.OnClickListener and override the method onClick and inside it use switch cases to switch between views and apply on clicks like this:
public class SampleActivity extends AppCompatActivity implements View.OnClickListener{
Button btnGo,btnEscape;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btnGo);
btnEscape= (Button) findViewById(R.id.btnEscape);
btnGo.setOnClickListener(this);
btnEscape.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnGo:
startActivity(new Intent(MainActivity.this, GoActivity.class));
break;
case R.id.btnEscape:
startActivity(new Intent(MainActivity.this, EscapeActivity.class));
break;
default:
break;
}
}
}
In code, what the two comments by Shiram and Nik above are saying is to add the block below that begins Button btnEscape... after Button btnGo.setOnclick's block.
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Button btnGo = (Button) findViewById(R.id.btnGo);
...
Button btnEscape = (Button) findViewById(R.id.<<name of button in xml>>);
btnEscape.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... whatever ... }
});
}
In short, the following is a really good pattern to have at your fingertips:
Button btn___ = (Button) findViewById(R.id.<<name of button in xml>>);
btn___.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... whatever ... }
});

How do I call the FileExplorer.java with a button from 2 available buttons?

I am still new to Java
I am trying to implement the FileExplorer! <<= link
I have 2 buttons, one of them is to call the ExplorerFile class. But it does not seem to work.
My first button seems to work though.
The followings are my code (buttons):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Status = (TextView)findViewById(R.id.app_status);
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.setPriority(500);
this.registerReceiver(mUsbReceiver, filter);
DeviceInformationContext = this;
//Button1
Button connectBtn = (Button)this.findViewById(R.id.connectBtn);
connectBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myMucNil.Muc_Config(19256, (byte) 0x8A);
}
});
myMucNil = new MUC_NIL(DeviceInformationContext);
//Button2
Button getPy = (Button) findViewById(R.id.getFilePy);
OnClickListener pyList = new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, FileExplorer.class));
}
};
getPy.setOnClickListener(pyList);
}
I thank you in advance for your kind help, if you would like to guide me.
Try this code...
Button getPy = (Button) findViewById(R.id.getFilePy);
getPy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), FileExplorer.class));
}
});
And remove following code.
Button getPy = (Button) findViewById(R.id.getFilePy);
OnClickListener pyList = new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, FileExplorer.class));
}
};
getPy.setOnClickListener(pyList);
Also you need to register FileExplorer.java file in AndroidMenifest.xml file.
using following code you register.
<activity android:name="FileExplorer"></activity>
before application tag is over.

Categories