Keeping activity - android

How to keep the button activity on background/pause or something like this when i press the physical back button . I want this activity to set on when i press again the button.(to show me what did i paint in last activity);
MainActivity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonSave=(Button)findViewById(R.id.buttonDraw);
buttonSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,ButtonDraw.class));
}
});
}
ButtonDraw :
public class ButtonDraw extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(com.example.myfirstapp.R.layout.paintingfile);
BlackPixel blackPixel;
blackPixel = new BlackPixel(this);
setContentView(blackPixel);
blackPixel.requestFocus();
}
}

In your MainActivity include this method:-
#Override
public void onBackPressed() {
startActivity(new Intent(this,ButtonDraw.class));
finish(); // this line depends on you whether you want to finish the MainActivity or not.
}
The above method gives you control over the back button of android.

Related

how to make image invisble in first activity when navigating from second activity to first activity

I have two imageviews in firstActivity(MainActivity) when i click signIn image then it moves to SignUp Activity...
here when i click on signUp image then again it will come to MainActivity..and here i have to make firstName image invisible..
public class MainActivity extends Activity {
ImageView firstName,signIn ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstName =(ImageView)findViewById(R.id.imageView1);
signIn =(ImageView)findViewById(R.id.imageView2);
signIn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),SignUp.class);
startActivity(intent);
}
});
}
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
String mm ="5";
Intent i= getIntent(); String s = i.getStringExtra("PrevAct");
if (mm ==s) {
firstName.setVisibility(View.GONE);
}
}
public class SignUp extends Activity {
ImageView signUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
signUp =(ImageView)findViewById(R.id.imageView3);
signUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("PrevAct","5");
startActivity(intent);
}
});
}}
While you try to launch the MainActivity Activity again, ensure that you re-use the same instance and not a new one for application better performance.
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("PrevAct","SignUP");
startActivity(intent);
Use a variable to check if the screen is rendered from SignUpActivity.
Intent i= getIntent();
String s = i.getExtra("PrevAct","NO");
Based on the String Value, you can decide to show/hide.
Please note that View.Invisible only hides the view from screen. But still it gets loaded and takes the space on screen. This is a bad UI implementation.
Therefore use View.GONE instead.
Your Source Code modified as below
public class MainActivity extends Activity {
ImageView firstName,signIn ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstName =(ImageView)findViewById(R.id.imageView1);
signIn =(ImageView)findViewById(R.id.imageView2);
signIn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),SignUp.class);
startActivity(intent);
}
});
}
protected void onStart() {
super.onStart();
firstName = (ImageView)findViewById(R.id.textView1);
{
Intent i= getIntent();
if(i!=null){
String s = i.getExtra("PrevAct","NO");
if(s.equalsIgnoreCase("SignUP"))
firstName.setVisibility(View.GONE);
}
else
firstName.setVisibility(View.Visible);
}
}
public class SignUp extends Activity {
ImageView signUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
signUp =(ImageView)findViewById(R.id.imageView3);
signUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("PrevAct","SignUP");
startActivity(intent);
}
});
}}
By clicking signIn image, use startActivityForResult to start your activity for signing up.
In signing up activity, use setResult and finish to return to login activity.
When returning to first activity, make firstName image invisible or gone as you like in onActivityResult.

BadTokenException in Asynctask progress dialog

i'm new to android,this maybe simple question but i cant able to find the answer for it,i'm using tabactivity while changing the activities the bottom tab's are missing so i
used below code to hold the tab by changing the view,so it works fine
public void replaceContentView(String id, Intent newIntent) {
try {
#SuppressWarnings("deprecation")
View view = getLocalActivityManager().startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
this.setContentView(view);
} catch (Exception e) {
e.printStackTrace();
}
}
Actually my problem is i want to select something from 2nd activity to 1st ActivityGroup,in the 2nd activity i have one button called done
Here is my 2nd activity
public class Woosuite_Twiter_Feed_list extends Activity implements
OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.woosuite_twitlist);
Button done = (Button) findViewById(R.id.btn1);
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(v.getContext(), Woosuite_Feed.class);
i.setClass(getParent(), Woosuite_Feed.class);
Woosuite_Feed feed = (Woosuite_Feed) getParent();
feed.replaceContentView("one", i);
}
});
while clicking done button in 2nd activity then first activitygroup asyncTask class is called,in that i'm getting
Android: “BadTokenException: Unable to add window; is your activity running?
i dont know how to solve this,please provide some solution.
here is my 1st ActivityGroup oncreate
public class Woosuite_Feed extends ActivityGroup implements OnClickListener
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.woosuite_feed);
LinkedFeeds feeds = new LinkedFeeds();
feeds.execute("");
}
Here is my 1st ActivityGroup AsyncTask
public class LinkedFeeds extends AsyncTask<String, Void, String> {
public ProgressDialog dialog;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
if(!isFinishing()){
dialog = new ProgressDialog(Woosuite_Feed.this);
dialog.setMessage("Loading...");
dialog.setCancelable(false);
dialog.show();
}
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
}
Thanks

how do i update the database table on the basis of checkbox/unchecked

how can i retrieve the checkboxes value to update the database table on pressing the update button...
TeacherLoggedInClass
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The isChecked() method return a boolean value with the information you need.
I would also suggest you use another way with implementing the Listener. This is what Google recommends:
public class MyActivity 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... for example your checkBox.isChecked()
}
});
}
If you want to get the single checkbox value and set database, you can follow below code,
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cbBox.isChecked()) {
// do your operation
} else {
// do your operation
}
}
}
if you need to use a list of checkbox status to update databases, you'd better to create a collection to cache every checkbox status , and with bulkInsert or transaction to commit.

Force close error

What is the force close problem of this program?
public class MyActivity extends Activity {
TextView t=(TextView)findViewById(R.id.textView1);
Button r=(Button)findViewById(R.id.button2);
private OnClickListener i=new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("fghffghfhgf");
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
r.setOnClickListener(i);
}
}
You need to get your TextView and Button after inflating the layout.
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here inflate the layout
setContentView(R.layout.main);
//now you can get your widgets
final TextView t= (TextView)findViewById(R.id.textView1);
Button r=(Button)findViewById(R.id.button2);
r.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("fghffghfhgf");
}
};
);
}
}
I really recommend you to check this to build your first app.

activity loss information

i writed an application which contains an option button and Option Activity.
In the OptionActivity, you can choose play a music "on" or "off" with togglebutton. and there are some check button also.
until here everything is okey but when you check some box and back to the main menu and back to the Options Activity again, you find that all variables is default value. for example the checked box one is uncheckhed. I know that it is because of everyCall the Options Activity with new Intent but i want to know that starting an activity "without using new Intent"
This is my SPLASH SCREEN
public class MainActivity extends Activity {
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baslangic);
Thread thred=new Thread(){
public void run(){
try {
sleep(5000);
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.OPTIONS"));
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
finish();
}
}
};
thred.start();
}
}
This is my MAIN ACTİVTY which contains options button
public class Baslangic extends Activity {
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start=(Button)findViewById(R.id.button1);
Button options=(Button)findViewById(R.id.button2);
Button about=(Button)findViewById(R.id.button3);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.OYUN"));
}
});
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
about.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.ABOUT"));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And This is My options Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
it=(RadioButton)findViewById(R.id.radio0);
ti=(RadioButton)findViewById(R.id.radio1);
final ToggleButton but=(ToggleButton)findViewById(R.id.toggleButton1);
music=MediaPlayer.create(Options.this, R.raw.avril);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(but.isChecked())
music.start();
else
music.pause();
}
});
it.setChecked(true);
Button menu=(Button)findViewById(R.id.button1);
menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
}
});
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStop() {
super.onStop();
}
}
NOTE: Be aware That when the user click the options Button, i call finish(), actually i have a solution. My solution that, after the splash screen, i start 2 activity; first Options later Menu, And when the user click the Options button and i call the finish() so i am in the options Button :D and when press back i called new Intent(menu) you can see on OPTIONS activity,
So my problem is already solved but i want to know that there should be a solution different from this way, If you know pls help me. :)
Are you using the PreferenceActivity or a normal Activity superclass?
Anyway, be sure to read about the SharedPreferences - it can store your values for entire app or for single activity.

Categories

Resources