public boolean onKeyDown(int keyCode, KeyEvent event){
if (isSub2&&keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(ctxx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
isReturning = true;
return false;
}
else {
return super.onKeyDown(keyCode, event);
}
}
}
There are two Activities Main--Sub2.
When you push a button in Main you can go to Sub2.
This code is in Sub2. I want to use back button on the bottom to make the MainActivity put on the top of stack not killing Sub2.
When I run it on the phone it works all right at first,
but after few more times of going back in Sub2 and going to Sub2 again
the back button stops working.
I don't know what is making the back button freeze.. any ideas?
ps) i've tried using handlers inside the method and overriding onBackButtonPressed() instead of using onKeyDown..
but no difference at all..
Not sure why your button freezes. It would be helpful to see what you are doing in the main activity. Here's an example that works for me and does not freeze:
You can put this in the main activity:
#Override
protected void onStart() {
super.onStart();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SubActivity.class);
//****** Uncomment the following line if you want to re-use the subactivity instead of launching a new one
//intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
});
}
And this in the sub-activity:
#Override
public void onBackPressed() {
//super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
Related
I want to create 2 or more than 2 two button in android but I got the problem in this line
View btnClick = findViewById(R.id.buttonClick);
View btnscan = findViewById(R.id.btscanClick);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
#Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.buttonClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
public void onClick1(View arg1) {
if(arg1.getId() == R.id.btscanClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
may be the problem would be with your design, I thing you are implementing it in surface view. if you do so make your layout with LinearLayout and RelativeLayout, it would be better if you also add your .manifestfile
Hey cast your views to Button i.e. instead of
View btnscan = findViewById(R.id.btscanClick);
use
Button btnscan = (Button)findViewById(R.id.btscanClick);
also post the errors you are getting so that I can help you out.
Add listener to other button as well
Button btnscan = (Button)findViewById(R.id.btscanClick);
btnscan.setOnClickListener(this);
Button btnclick = (Button)findViewById(R.id.buttonClick);
btnclick.setOnClickListener(this);
And let handle the click with only one OnClick method.
public void onClick(View arg0) {
if(arg0.getId() == R.id.buttonClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
else if(arg1.getId() == R.id.btscanClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
}
}
The way you usually want to go to define the OnClickListener for buttons is as follow :
btnClick.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
});
btnScan.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
//define a new Intent for the scan Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
}
});
This means that you use a different onClickListener for each button.
I want to make buttons once I click on the button I go to another activity?
and the problem is only the first button is working!
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageOne.class);
v.getContext().startActivity(myIntent);
Button PageTwo = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageTwo.class);
v.getContext().startActivity(myIntent);
}
{}
});
}
});
}
}
Think it is because most of your code is closed inside the scope of the first onClickListener, try something like this.
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
});
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
});
Using v.getContext() should be ok, this is just how I usually would do as the Activity itself is indeed a valid context. I guess it just seems more readable to me.
Edit:
Just as a clarification to the current state of your code. The second button is assigned a onClickListener only after the first button is pressed. But since the first button takes the app to a new Activity, inherently destroying the Main Activity, the second button will never have a chance to reach it's onClickListener.
Hope it makes sense, nevertheless the code above should fix the issue.
There are a couple of issues currently in your code. The first issue is that your second button is being defined inside the first button's declaration. The next issue is that you're setting the second OnClickListener to the wrongly named button. You've made a typo and instead of PageTwo, which you've called the Button (presumably you wanted to call it PageTwoButton in accordance with the first Button) and then set the OnClickListener to PageTwoButton instead. Seeing as you're also using multiple Buttons, it's a lot cleaner and more efficient to use a GroupOnClickListener. I'd probably also suggest using 'this' instead of 'v.getContext()' as well when setting up your Intents. Change your code to be like so:
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageOneButton.setOnClickListener(addGroupOnClickListener);
PageTwoButton.setOnClickListener(addGroupOnClickListener);
private OnClickListener addGroupOnClickListener = new OnClickListener() {
public void onClick(View v) {
if (v == PageOneButton) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
} else if (v == PageTwoButton) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
}
}
};
Hope this helps!
Two words: Code Indentation
Were you to indent your code properly, you would have noticed that you're setting OnClickListener INSIDE your first buttons' listener. Move it outside your first listener, as has already been advised by others.
There's also an extra pair of {}, which is redundant.
Also, #edwoollard noticed that for the second button, you're using two different names, PageTwo and PageTwoButton. Keep that in mind, unless it's a typo.
I have a MainActivity and I am posting partial code here:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_quit:
moveTaskToBack(true);
return true;
case R.id.action_help:
//display the help activity
Intent myIntent = new Intent( this, ShowHelp.class);
startActivityForResult(myIntent, 0);
return true;
case R.id.action_about:
//display the about window
//aboutApp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void aboutApp() {
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.about);
dialog.setTitle("About TollCulator");
TextView textViewAbout = (TextView) dialog.findViewById(R.id.tvContents);
textViewAbout.setText("About: ");
textViewAbout.append("\n\tTollCulator - Toll Calculator");
textViewAbout.setMovementMethod(new ScrollingMovementMethod());
Button btnCloseIt = (Button) dialog.findViewById(R.id.btnOk);
// if button is clicked, close the custom dialog
btnCloseIt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
ImageView iv1 = (ImageView) dialog.findViewById(R.id.imgFB);
iv1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("https://www.facebook.com/PagesByZ"););
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
dialog.cancel();
startActivity(intent);
}
});
dialog.show();
}
ShowHelp.class partial code here which is an Activity on it's own:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case 16908332:
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
//finish();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
// do something on back.
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
//finish();
return;
}
Now when i choose action_help from the menu it opens the ShowClass Activity and I can press the back button and it brings me back to MainActivity without any problem.
Now If I choose action_about the About Dialog pops up and If I click on the icon within the Dialog the following pops up:
At this time If I decide to press BACK, instead of closing the pop and displaying my About Dialog, I am just taken to the Homescreen.
Commenting the finish(); method in ShowClass allows me to click the BACK button from the "Complete action using" But instead of taking me to the MainActivity, it takes me to ShowClass from there I can press the Back button again to come back to MainActivity. So as long as I choose action_help first, pressing back brings me back to the ShowHelp.
When the Complete action using pops up from the About Dialog, I set up a Log, and it's on the onPause() state and if I did not run ShowHelp in the beginning, my App goes directly to onStop() and onDestroy().
Please help me fix the issue.
To test it, I created a demo without the ShowHelp class with just the dialog and By pressing back button, the Complete actiong using dialog closes and then pressing back again closes the About Dialog. So I am not sure what is going on.
Declare a global boolean variable chooserIsShowing:
boolean chooserIsShowing;
Set it to true here:
ImageView iv1 = (ImageView) dialog.findViewById(R.id.imgFB);
iv1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set the flag
chooserIsShowing = true;
Uri uri = Uri.parse("https://www.facebook.com/PagesByZ"););
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
dialog.cancel();
startActivity(intent);
}
});
Change the overriden onBackPressed() method:
#Override
public void onBackPressed() {
if (!chooserIsShowing) {
// do something on back.
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
finish();
}
// remove the flag
chooserIsShowing = false;
return;
}
Edit 1:
Declare dialog as a global variable:
Dialog dialog;
Change aboutApp():
public void aboutApp() {
dialog = new Dialog(this);
....
}
Override onKeyDown(int, KeyEvent) method in MainActivity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
You can remove chooserIsShowing as its not required.
Final Solution:
A noHistory flag was defined for this activity in the manifest. Removing that and fixing onBackPressed() method of ShowHelp resolved the issue.
EDIT: onClick is working properly now. The issue was that the button was trying to fire the onClick from the Parent class. now that is fixed. Of course that means a new issue is happening, that is the onActivityResult is never getting called.
So I am not really sure what the hell is going on, when I hit the button nothing happens, nothing in logcat, nothing, as if there is not code, but I am pretty sure this is written correctly, any thoughts?
public class myClass extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.store_selector);
Button getStore = (Button)findViewById(R.id.getStore);
getStore.setOnClickListener(buttonGetStoreOnClickListener);
}
Button.OnClickListener buttonGetStoreOnClickListener
= new Button.OnClickListener(){
public void onClick(View arg0) {
Intent intent = new Intent("com.blah.Blah.client.android.SCAN");
intent.setPackage("com.blah.Blah");
intent.putExtra("com.blah.Blah.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
};
};
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.i("debug tag", "contents: "+contents+" format: "+format);
Intent myIntent = new Intent(this, Ads.class);
myIntent.putExtra("key", contents);
startActivity(myIntent);
setContentView(R.layout.activity_ads);
// Handle successful scan
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
}
};
This is the class you need to import:
import android.view.View.OnClickListener;
And this is the method you should have:
OnClickListener onButtonListener = new OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
};
Test it and let me know if it worked.
Regards
Are you sure you got the right button with Button getStore = (Button)findViewById(R.id.getStore); ?
If yes, then it can be something that sometimes happens to me sometimes.
When this happens, my logcat doesn't show anything. What I do to solve this, is to open the Devices view (Window, show view, other, Android, Devices) and select my device. Then when I look at the logcat again, everything's there.
How to navigate from one Activity screen to another Activity screen? In the first screen I'm having one button if I click the button it has to move to another Activity screen.
The most trivial case (called from activity):
startActivity(new Intent(this, ActivityToLaunch.class));
More details here: http://developer.android.com/guide/topics/fundamentals.html
OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(action));
}
};
Button button = (Button) findViewById(id);
button.setOnClickListener(onClickListener);
Button x.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent i = new Intent(y.this, Activity.class);
startActivity(i);
}
});
Here we've defined a listener for Button x. The OS will call this method and start the Activity referenced in Intent i.
Here's the official tutorial example:
http://developer.android.com/guide/tutorials/notepad/notepad-ex2.html
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(TestActivity.this,second.class));
}
});
public void onClick(View v)
{
Intent myintent = new Intent(currentclass.this, nextactivity.class);
startActivity(myintent);
}
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent) -- Used to launch a new activity.
So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --
PresentActivity -- This is your current activity from which you want to go the second activity.
NextActivity -- This is your next Activity on which you want to move.
So the Intent would be like this
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities.
final Context cont = this;
Button btnClickABC =(Button)findViewById(R.id.btnClickABC);
btnClickABC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(cont, NextActivity.class));
}
});
Use following code..I hope this will help you.
Button button = (Button)findViewById(R.id.xxx);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
startActivity(intent);
}
});
xxx is id from your xml of your Button.
startActivity(new Intent(this,newActivity.class));
Switching from one activity to another is really simple, but tricky for a new one.
Your next class must be defined in AndroidManifest.xml. This is tester class:
<activity
android:name=".Tester"
android:label="#string/title_activity_tester" >`enter code here`
</activity>
final Button button = (Button) findViewById(R.id.btnGo);// btnGo is id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(CurrentClass.this, Tester.class);
startActivity(i);
}
You can navigate to the next screen using these code snippets:
Kotlin
startActivity(Intent(this, LoginActivity::class.java))
Java
startActivity(new Intent(this, LoginActivity.class))
Here's a reference: Android Developers - Starting another activity
Intent intentobj=new Intent(FromActivity.this,ToActivity.class);
startActivity(intentobj);
or you can simply use
startActivity(new Intent(FromActivity.this,ToActivity.class));
For Kotlin (if you are in an activity)
buttonToClick.setOnClickListener{ startActivity(this,YourDestinationActivity::class.java)
}
If you are in a fragment
buttonToClick.setOnClickListener{
startActivity(requireActivity, YourDestinationActivity::class.java)
}
In your method fun onCreate(savedInstanceState: Bundle?) add this.
your_btn_id.setOnClickListener{
val intent = Intent(this, yourpagename::class.java)
startActivity(intent)
}
Until now if it doesn't work then, check if these two files are added or not,
import android.content.Intent
import kotlinx.android.synthetic.main.activity_otp.*
Try this code:
Button my_btn;
my_btn = findViewById(R.id.submit_btn);
my_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_2);
}
});
Button navigate;
navigate = findViewById(R.id.button);
navigate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Navigate another Activity",Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
});
Just go to XML file and add Onclick = "opennewactivity" in button xml.
Then go to java code and create class opennewactivity you can just make it my clciking alt+Enter in xml code's "opennewactivity". in that just write
Intent intent = new Intent(this, newacivity.class);
startActivity(intent);
and if you want user to not get back to first activity again then just write this
Intent intent = new Intent(this, newactivity.class);
startActivity(intent);
finish();
Cartoon_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
newActivity();
}
});
}
public void newActivity()
{
Intent selectClass= new Intent(getApplicationContext(), com.example.fyp.videoplayer.class);
startActivity(selectClass);
}