I want to write some code that performs the action to go back to the previous activity. Of course I can write a code for each back button that I have, but I would prefer to have only a piece of code working for all the back buttons (and then, for example, to assign the function to the "onclick" event). How can I manage that?
Please use onclick method and that method redirect to previous activity.
#Override
public void onBackPressed()
{
// your code.
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN)
{
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Use inheritance
YourActivity.java
public class YouActivity extends BaseActivity {
Button back_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
back_button = (Button) findViewById(R.id.back_button);
back_button.setOnClickListener(base_listener);
}
}
BaseActivity.java
public class BaseActivity extends Activity {
View.OnClickListener base_listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//you back action here;
}
};
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
Related
My splash screen diseapear after 2 seconds but I want it to diseapear also after a screen touch
Like if I press screen I moved next page without waiting until the end of the timer.
Is it possible??
public class SplashActivity extends Activity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
},2000);
}
}
Tes it is possible, you can add a touch listener to your layout that do the same as what will happen after two seconds , some thing like that
findViewById(R.id.myLayout).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return true;
}
});
EDIT :
or you can instead add the next code outside onCeate method if the layout don't receive the touch event:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return super.dispatchTouchEvent(ev);
}
I have two Classes "BaseActivity" and "ChildActivity" i.e. ChildActivity inherts BaseActivity.
Question: In my following Code Snippet, whenever i press LEFT BUTTON - it logs me "I am From Child Activity". What would i need to do if i want to call SUPER CLASS functionality by default.
public class BaseActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
};
protected void configureTitleBar(String title) {
ImageButton imgLeftButton = ((ImageButton) findViewById(R.id.actionBarLeftButton));
imgLeftButton.setOnClickListener(BaseActivity.this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.actionBarLeftButton){
printCustomLog("I am From Base");
}
}
}
Child Activity:
public class ChildActivity extends BaseActivity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
configureTitleBar("MyTitle");
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.actionBarLeftButton){
printCustomLog("I am From Child Activity");
}
}
}
If you want to get super class functionality, you can
a) Not Override the onClick() method at all (but I don't think that's what you want)
b) Call super.onClick(v) from onClick() in your child class.
The code in your ChildActivity will then be.
#Override
public void onClick(View v) {
// Check some condition if you want to handle it in Child class
if(condition){
printCustomLog("I am From Child Activity");
}
// Else, as default, call Base class's onClick()
else{
super.onClick(v);
}
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.actionBarLeftButton) {
// here's my work
}
super.onClick(v); // it will call Super's OnClick
}
I want to implement the ...
#Override
public void onBackPressed() {
}
However, I get an error message saying, "Annotations are not allowed here". I need this method to be implemented here. Is there an alternative?
public class supbreh extends Appbreh
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_breh);
if (myBundle != null) {
String name = myBundle.getString("workout");
ShowDetails(name);
}
}
private void ShowAbDetails(String mName) {
if(mName.equals("abs1")){
#Override
public void onBackPressed() { //"Not Allowed here"
}
}
void onBackPressed ()
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.
In here you can't declare this method inside another method .
Only override it in that one Activity
#Override
public void onBackPressed()
{
super.onBackPressed();
}
FYI
#Override
public void onBackPressed() {
Intent intent = new Intent(IndividualAbsWorkout.this, IndividualAbsWorkout.class);
startActivity(intent);
}
You can override onBackPressed as normal and call the method in ShowAbDetails() method like below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_breh);
if (myBundle != null) {
String name = myBundle.getString("workout");
ShowDetails(name);
}
}
private void ShowAbDetails(String mName) {
if(mName.equals("abs1")){
onBackPressed();
}
}
#Override
public void onBackPressed() {
// your logic here
}
I have an Activity with one Fragment. This fragment has one button, and I want something to happen when I push the "enter" button on my hardware keyboard (I am testing it with adb keyevent).
I've read a lot of solutions here on stackoverflow, but none of them worked.
This is my fragment code:
btnPhoto = (Button) rootView.findViewById(R.id.taskBtnPhoto);
photoView = (ImageView) rootView.findViewById(R.id.taskPhotoView);
btnPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhoto();
}
});
btnPhoto.setFocusable(true);
btnPhoto.setFocusableInTouchMode(true);
btnPhoto.requestFocus();
btnPhoto.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_VOLUME_UP == keyCode) {
doFoo();
return true;
}
return false;
}
});
This code is placed in the onCreateView part of my fragment. Using debug, the event is never fired when I launch KeyEvent from ADB or use the hardware keys of my phone (volume keys).
public static class MyFragment extends Fragment {
#Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//YOUR CODE
return false;
}
});
}
}
All I want to do is programatically add an onKeyDown listener to an existing activity. A little bit of context for what I want to do: I want to make a standalone function that handles click events that occur in a mediaplayer.
ie: Let's pretend I have this class.
public class Main extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maingui);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
}
return false;
}
}
What I would like to do is add a function that adds the onKeyDown Listener through programming.
ie:
public class Main extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maingui);
createListener();
}
}
public void createListener()
{
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
}
return false;
}
}
This obviously doesn't work, but gives you a good idea of what I'm trying to do.
This is what I've tried which doesn't work.
public class Main extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maingui);
MyKeyClickClass.createListener(this);
}
}
Then Imagine this function in a MyKeyClickClass class
public static void createListener(Activity act)
{
View testing = act.getWindow().getDecorView().findViewById(android.R.id.content);
testing.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
String test = "Hi";
return false;
}
});
}
Is this possibly to do? I'm not getting any response to key events doing it this way.
Cheers
If for whatever reason you can't just use onKeyDown, couldn't you just have onKeyDown pass its parameters to another function?
If you create a myKeyListener class then you can do something like this.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
myKeyListener.onKeyDown(keyCode, event);
}