starting an activity where user left it - android

I have the following code:
public class SplashScreenActivity extends Activity {
private boolean animated ;
private Handler handler1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isPreviouslyLoggedIn()) {
setContentView(R.layout.splash);
final TextView revolution=(TextView) findViewById(R.id.textView1);
final Button login=(Button) findViewById(R.id.loginButton);
final Button signUp=(Button) findViewById(R.id.signUpButton);
login.setOnClickListener(loginListener);
signUp.setOnClickListener(signUpListener);
if (!animated) {
animated = true;
revolution.setVisibility(View.INVISIBLE);
login.setVisibility(View.INVISIBLE);
signUp.setVisibility(View.INVISIBLE);
ImageView image = (ImageView) findViewById(R.id.image);
TranslateAnimation slide = new TranslateAnimation(0, 0, 100, 0);
slide.setDuration(1000);
image.startAnimation(slide);
handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
revolution.setVisibility(View.VISIBLE);
login.setVisibility(View.VISIBLE);
signUp.setVisibility(View.VISIBLE);
}
},1200);
}
}
else {
setContentView(R.layout.home);
Intent intent = new Intent(getApplicationContext(), PickUpActivity.class);
startActivity(intent);
}
}
When the user clicks on one of the buttons, it leads him to a different activity in the same app. However, when the user clicks back from the next activity, the animation is started again. How can I prevent the animation from showing again as i want it to occur only once when user opens the app?

You can use Application class.Declare a boolean var in it and set it "true" before starting second Activity.In your onCreate() of first Activity check this boolean and do animation only it is false(it means that user has not started second activity yet).For example create class with name App in your package:
public class App extends Application{
private static boolean animated;
#Override
public void onCreate() {
super.onCreate();
animated = false;
}
public static boolean getAnimated(){
return animated;
}
public static void setAnimated(boolean animated1){
animated = animated1;
}
}
Register App in manifest:
<application
android:icon= ...
android:label= ...
android:name="yourpackage.name.App" >
(I suppose that your package name is :"yourpackage.name")
Now change your code like this:
if (!App.getAnimated()) {
App.setAnimated(true);
revolution.setVisibility(View.INVISIBLE);
login.setVisibility(View.INVISIBLE);
signUp.setVisibility(View.INVISIBLE);
...
Or you can use sharedpreferences and retrieve a boolean from it when you want to start animation.You have to set it's default value "false" and when user start second Activity,you have to set it "true".

You can set a flag for this, first time keep the flag true and when the user clicks on any of the button, set the flag value to false.
Now start the animation if the flag value is true.

Related

how to pop up a dialog box for the first time when i launched my app for the first time?

i want to pop up a dialog box when my app is open for the first time in a device.i want to show in popup box that how to use the app.
if the app is opening for the first time it will show the dialog box ,otherwise it will simply avoid to show the dialog box.and the activities also will change according to the first use or normal use.for first time use it will show a activity1 or else it will show activity2.please help me.
this is my activity which show a image when app is opened
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_name);
new Timer().schedule(new TimerTask() {
public void run() {
// here i want to go to another activity acording to the first time use or normal time
}
}, 3000);
}
public class class_name extends AppCompatActivity {
public static final String MyPREFERENCES2 = "MyPrefs" ;
SharedPreferences sharedpreferences2;
public boolean isFirstRun;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_name);
new Timer().schedule(new TimerTask() {
public void run() {
checkFirstRun();
}
}, 3000);
}
public void checkFirstRun() {
System.out.println("its in check first run");
isFirstRun = getSharedPreferences("PREFERENCE2", MODE_PRIVATE).getBoolean("isFirstRun", true);
if (isFirstRun){
startActivity(new Intent(class_name.this, new_activity1.class));
getSharedPreferences("PREFERENCE2", MODE_PRIVATE)
.edit()
.putBoolean("isFirstRun", false)
.commit();
}
else{
startActivity(new Intent(class_name.this, new_activity2.class));
}
}
}
final String FIRST_TIME_KEY = "com.example.app.MainActivity.firstTimeKey";
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstTime = sp.getBoolean(FIRST_TIME_KEY, false);
if(isFirstTime) {
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean(FIRST_TIME_KEY, true);
edit.apply();
//show the dialog
}

how help screen show for one time only in android

in the following code work properly and show help screen when open activity but I want show one time forever,
what can i do?
What should I add in the code?
my code:
public class KhatmMain extends Activity implements OnClickListener{
Context ctx;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.khatmmain);
showOverLay();
.
.
.
}
private void showOverLay(){
final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_view);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.overlayLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
}
You can use SharedPrefereces to set a variable that will check if you've shown the dialog yet to the user or not, here's an example:
SharedPreferences prefs = this.getSharedPreferences("com.you.app", Context.MODE_PRIVATE);
Boolean dialogShown = prefs.getBoolean("dialogShown", false);
Then check if the value of dialogShown is false (you don't need to set it first since it will default to false the way we are calling it), then on the following code we execute some code, only if dialogShown is false, meaning we can do all the dialog stuff inside that conditional:
if(!dialogShown){
//Your show dialog code
prefs.edit().putBoolean("dialogShown",true).commit();
}
So the next time we check for the dialogShown value on the shared preferences it will be true therefor not showing the dialog. I believe this is the most common way of doing it.
There is a solution ..
when application first time start then save the shared preference to the app..
Now each and every time You retrieve the shared preference and check if it is there then move to next screen
Use this code:
public class KhatmMain extends Activity implements OnClickListener{
Context ctx;
Boolean showOneTime = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.khatmmain);
showOverLay();
.
.
.
}
private void showOverLay(){
if (showOneTime == false) {
return;
}
final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_view);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.overlayLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
showOneTime = false;
}
}

How to save activity state after navigating?

I have an app with 3 activities,home,calculationResult and help.What I'm trying to do is save the details of the calculations on calculationResult when the user navigates to help.So when the user is in help and presses the action bar back icon,the results of the calculation will still be there in calculationResult.
I have tried to implement this so far by following this guide: Recreating an activity,But when I implemented it the variable I'm wanting to store is not recognized when using with savedInstanceState.Below is how I have tried to do this in the result class.Can someone point out where I have gone wrong with this or if this is the correct way to accomplish saving the activity state?
public class CalcResult extends Activity implements OnClickListener{
TextView result1;
static final String MARK1 = "marking1";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
if (savedInstanceState != null) {
// Restore value of members from saved state
//not recognizing this variable mark1 which I'm setting to the variable that stores the result of the calculation.
mark1 = savedInstanceState.getDouble(MARK1);
}
final Intent intent1=new Intent(this,AboutActivity.class);
final Intent intent2=new Intent(this,MainActivity.class);
final Intent intent3=new Intent(this,MainActivity.class);
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.a,
null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
final Button actionBarHome = (Button) findViewById(R.id.action_bar_title);
actionBarHome.setBackgroundResource(R.drawable.ic_action_back);
actionBarHome.setOnClickListener(this);
actionBarHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intent2);
}
});
final Button actionBarInfo = (Button) findViewById(R.id.action_bar_staff);
actionBarInfo.setBackgroundResource(R.drawable.ic_action_help);
actionBarInfo.setOnClickListener(this);
actionBarInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intent1);
}
});
final Button actionBarHoome = (Button) findViewById(R.id.action_bar_home);
actionBarHoome.setBackgroundResource(R.drawable.appicon);
actionBarHoome.setOnClickListener(this);
actionBarHoome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intent3);
}
});
result1 = (TextView)findViewById(R.id.markOne);
Intent intent = getIntent();
double markOne = intent.getDoubleExtra("number1", 0);
DecimalFormat df = new DecimalFormat("#.##");
result1.setText(String.valueOf(df.format(markOne)+"mm"));
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
//Also doesn't recognise markOne here ->
savedInstanceState.putDouble(MARK1, this.markOne);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
Instead of this
double markOne = intent.getDoubleExtra("number1", 0);
You should write
markOne = intent.getDoubleExtra("number1", 0);
by declaring it again you are not assigning the value to the class level markOne
Also you can try setting the lauchmode of your calculateResult activity as singleTop
android:launchMode="singleTop"
This will use the same instance of the activity that already exist on the top of the stack so will have the same state as before.
Try calling finish in your Help activity when you move to CalculationResult activity.
for ex:
StartActivity(<Intent>);
finish();
onRestoreInstanceState() is called when Activity was killed by the OS. "Such situation happen when:
•orientation of the device changes (your activity is destroyed and recreated)
•there is another activity in front of yours and at some point the OS kills your activity in order to free memory.
Next time when you start your activity onRestoreInstanceState() will be called."
But in your case, this might not happen.
Approach i followed
I set a global varibale to act as a flag if i am launching this activity for the first time. If the global varibale is the same as what i had set, i leave the editText untouched. (in your case, result1). If the value is changed, i set the editText for this value. If the user clicks the editText even once, i track the change and store the value. When you think, the mark1 is no longer needed, you can set the value of flag again as "FIRSTENTRY". This would work.
Kindly try and let us know if you still face issues.
Step 1
Created a class to store a static Global variable.
public class Constants {
public static String sFlag= "FIRSTENTRY";
}
Step 2
Add this piece of code after "setContentView(R.layout.result);" line in your oncreate method. Instead of TextView, i have declared result1 as EditText.
if(!Constants.sFlag.equalsIgnoreCase("FIRSTENTRY"))
{
result1.setText(Constants.sFlag);
}
result1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
Constants.sFlag = result1.getText().toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Constants.sFlag = result1.getText().toString();
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Constants.sFlag = result1.getText().toString();
}
});

How to get child Activity View in Parent Activity Class(Android)?

I implemented a bottom button bar(something like a tab bar controller in iPhone). For this I created a common layout (button_bar.xml, 5 image buttons) and included in other activity xml files. and for the managing the click action I created a BaseActivity.java extended from Activity and perform the click actions. and I extend other activity which need button bar from this BaseActivity, which works fine. Now I want to include a selected state to these buttons, but when I access the buttons in base activity it give a null pointer error. How can I solve this.
button_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottomButtonBar"
style="#android:style/ButtonBar"
... >
<ImageButton
android:id="#+id/btnGuests"
android:onClick="showAllGuests"
android:src="#drawable/ic_guest_list" />
<ImageButton
android:id="#+id/btnAddGuest"
android:onClick="selectGuestType"
android:src="#drawable/ic_add_guest" />
<ImageButton
android:id="#+id/btnAddParty"
android:onClick="showAddParty"
android:src="#drawable/ic_add_party" />
....
</LinearLayout>
public class BaseActivity extends Activity {
// common to other activities.
public void showAddParty(View view) {
//showAddParty is one one of the buttons click method. 4 more buttons are there
Intent intent = new Intent(this, AddPartyActivity.class);
startActivity(intent);
// I can get "view.findViewById(R.id.btnAddParty)" here
// but I can't get "findViewById(R.id.btnAddGuest)" here. how this possible
}
}
public class AddPartyActivity extends BaseActivity{
....
}
Here i can get the corresponding view from parameter view, and change backgroundIbageSource. but when it goes to "AddPartyActivity" that inherited from Baseactivity, the new image is replaced by old one. How can I implement the selected feature in BaseActivity itself?
you can hold the state of the Button(clicked or not) using booloean in BaseActivity and check the value of these variables in AddPropertyActivity's onCreate() method.
public class BaseActivity extends Activity {
protected static boolean isButton1Clicked = false;
protected static boolean isButton2Clicked = false;
protected static boolean isButton3Clicked = false;
protected static boolean isButton4Clicked = false;
.....
public void showAddParty(View view) {
isButton1Clicked = true;
//showAddParty is one one of the buttons click method. 4 more buttons are there
Intent intent = new Intent(this, AddPartyActivity.class);
startActivity(intent);
}
}
public class AddPartyActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
Button button1 = (Button)findViewById(R.id.button1);
if(isButton1Clicked) {
button1.setBackground(R.drawable.clicked);
} else {
button1.setBackground(R.drawable.not_clicked);
}
......
......
}
}
and remember that all boolean variables must be static variables. (every object will have it's own copy of instance variables but all objects will share a single copy of static members).
public class BaseActivity extends Activity {
protected static boolean isButton1Clicked = false;
protected static boolean isButton2Clicked = false;
protected static boolean isButton3Clicked = false;
protected static boolean isButton4Clicked = false;
protected Button button1;
protected Button button2;
protected Button button3;
protected Button button4;
protected void checkButtonsState() {
if (isButton1Clicked) {
button1.setBackgroundResource(R.drawable.image1);
} else {
button1.setBackgroundResource(R.drawable.image2);
}
..........
}
public void showAddParty(View view) {
isButton1Clicked = true;
// showAddParty is one one of the buttons click method. 4 more
// buttons are there
Intent intent = new Intent(this, AddPartyActivity.class);
startActivity(intent);
}
}
public class AddPartyActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.item1);
.....
checkButtonsState();
}
}
Your error come from because you are using view.findViewById(), but the view variable correspond of the object that catch the click event (look in your xml, the 'android:onClick="showAddParty"').
So, you have in parameter a view instance that point directly to the btnAddParty button.
Is why you can access with findViewById to btnAddParty id but not btnAddGuest id.
You can access to all the view hierarchy by using direct findViewById as method of the activity class:
View btAddParty = findViewById(R.id.btnAddParty)
View btAddGuest = view.findViewById(R.id.btnAddGuest)
Now, you can have a full solution, by implement a special method (setMyInternalContent in the sample below) in the BaseActivity and keep an instance of each button, after, you just have to update there state on click action:
public class BaseActivity extends Activity {
protected Button mBtAddParty;
protected Button mBtAddGuest;
// ...
protected void setMyContentView(int resId) {
setContentView(resId);
mBtAddParty = (Button)findViewById(R.id.btnAddParty);
mBtAddParty = (Button)findViewById(R.id.btnAddGuest);
// ...
}
public void showAddParty(View view) {
mBtAddParty.setClickable(true);
mBtAddGuest.setClickable(false);
// ...
//showAddParty is one one of the buttons click method. 4 more buttons are there
Intent intent = new Intent(this, AddPartyActivity.class);
startActivity(intent);
}
}
public class AddPartyActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setMyContentView(R.layout.activity_layout);
// Do not forget to set initial button state:
mBtAddParty.setClickable(true);
mBtAddGuest.setClickable(false);
// ...
}
}

Animating (moving) ImageView works one way but not the other

I creating a "Add" button by using sdk, so i wan it to move it to left when i press that "Add" button, and come back to original place when i press again. The problem i have is the button only can go to the left when press, and cannot come back to the original place after i trying to press it again. Does it i having some problem on the if...else function there? any help would appreciate.
public class TestImage2 extends Activity {
/** Called when the activity is first created. */
ImageView image_plus;
ImageView image_subtract;
ImageView image_multiply;
ImageView image_div;
String op;
boolean pergi = false;
final Animation animation = new TranslateAnimation(0,100,0,0);
final Animation animation2 = new TranslateAnimation(100,0,0,0);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image_plus = (ImageView)findViewById(R.id.test_image);
image_subtract = (ImageView)findViewById(R.id.test_image2);
image_multiply = (ImageView)findViewById(R.id.test_image3);
image_div = (ImageView)findViewById(R.id.test_image4);
image_plus.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
if(pergi == false)
{
animation.setDuration(1000);
image_plus.startAnimation(animation);
pergi= true;
animation.setFillAfter(true);
}
else
{
animation2.setDuration(1000);
image_plus.startAnimation(animation2);
pergi = false;
animation2.setFillAfter(true);
}
}});
i think that there is something wrong in
final Animation animation2 = new TranslateAnimation(100,0,0,0);
it should be
final Animation animation2 = new TranslateAnimation(0,-100,0,0);
:)

Categories

Resources