I have two activities. Whenever I go from the 1st activity to the 2nd activity count should be incremented on button click.
Here is the code in the 1st activity code for starting the 2nd activity:
imgCreatePost.setVisibility(View.VISIBLE);
imgCreatePost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i = new Intent(1st.this, 2nd.class);
//finish();
startActivity(i);
}
});
Here is where the count is incremented onCreate()
in the 2nd activity:
visitCount = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_post);
int increament = visitCount +=1;
Log.e(" increament ", " = " + increament);
if (visitCount == 10) {
visitCount = 0;
}}
imgBackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i = new Intent(2nd.this , 1st.class);
//finish();
//startActivityForResult(i, 9);
startActivity(i);
overridePendingTransition(R.anim.back, R.anim.back_out);
}
});
Whenever I come to the 2nd activity from the 1st activity increment is always 1. Why doesn't it increment one by one?
static visitCount = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_post);
//int increament = visitCount +=1;
visitCount++;
Log.e(" increament ", " = " + increament);
if (visitCount == 10) {
visitCount = 0;
}}
imgBackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i = new Intent(2nd.this , 1st.class);
//finish();
//startActivityForResult(i, 9);
startActivity(i);
overridePendingTransition(R.anim.back, R.anim.back_out);
}
});
You need to store "count" in SharedPreferences. This way, your "count" will persist event if the app gets restarted.
In the 2nd Activity onCreate() method, add:
//Retrieve last visit count from shared preferences
SharedPreferences prefs= getPreferences(MODE_PRIVATE);
int visitCount = prefs.getInt("VISIT_COUNT", 0);
visitCount++;
if(visitCount == 10){
visitCount = 0;
}
//Store visit count in SharedPreferences
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt("VISIT_COUNT",visitCount);
editor.apply();
You have to make static variable for counting that can be you can count increment. See below code may help you
static visitCount ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_post);
if(visitCount == 0){
visit=0;
}else{
int increament = visitCount +=1;
Log.e(" increament ", " = " + increament);
}
if (visitCount == 10) {
visitCount = 0;
}}
imgBackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i = new Intent(2nd.this , 1st.class);
//finish();
//startActivityForResult(i, 9);
startActivity(i);
overridePendingTransition(R.anim.back, R.anim.back_out);
}
});
for That you have to take static
variable visitCount
in first activity. and in onCreate()
of second activity you have to increment counter visitCount
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.