Android skip Activity and put it last - java

I am trying to make a skip button and it have to do what i draw.
i honestly don't know how can i make this happen.
The code actually have to skip the current activity and put it as last.
if the activity's question was answered, there is no skip.here i draw
I have tried a few things but it's beyond my powers.
Intent intent=new Intent(this, c1_2.class);
startActivity(intent);
finish();

you can use arrayList or Queue for keep your activityes, then use it to start Activity.
try this.
final ArrayList<Class> activities = new ArrayList<>();
// add your activity---------------
activities.add(c1_1.class);
activities.add(c1_2.class);
activities.add(c1_3.class);
//----------------------------
butonSkip=findViewById(R.id.skip);
butonSkip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Class firstActivity = activities.get(0);
startActivity(new Intent(c1_1.this,activities.get(0)));
activities.remove(0);
activities.add(firstActivity);
}
});
//-----------can change activities data
// activities.set(1, c1_3.class);

Related

onBackPressed() Best Practice/Performance

I usually override onBackPressed() like this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
finish();
}
Only now I saw that when I click the back button with this code I see for 0.5 sec a white activity in the transition.
Testing a little bit I found that if I use this code instead the problem didn't happen:
#Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
finish();
super.onBackPressed();
}
What's the difference between this two code? If I use the second one is fine? Cause any memory problem? Thanks
super.onBackPressed just calls finish. It isn't needed if you're calling finish yourself. Just remove the line.
The reason you may see a visual difference is that in one you're finishing this intent then starting a new one, vs starting a new one then finishing this one. The first may leave a blank screen briefly.

Switching activities loses functionality

I have an app that consists of 3 Activities
MainActivity
CalculatorActivity
InformationActivity
My MainActivity has a confirm button that onClick starts the CalculatorActivity and everything is done correct and working as intended.
CalculatorActivity has 2 buttons, one calculateButton that checks something and shows a message and a learnMorebutton that starts the InformationActivity.
When I am on the
CalculatorActivity for the first time everything is fine and working.Pressing the learnMoreButton navigates me to the InformationActivity.That activity looks like this :
InformationActivity:
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchActivity();
}
});
}
public void switchActivity(){
final Intent intentObj = new Intent(this,CalculatorActivity.class);
startActivity(intentObj);
}
A goBack button that gets me back to CalculatorActivity.Going back seems to break the functionality.Although the layout is there and everything looks as it should be, pressing the buttons (calculateButton,learnMoreButton) does nothing.
CalculatorActivity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
final Button calculateButton = (Button) findViewById(R.id.calculateId);
final Button learnMoreButton = (Button) findViewById(R.id.learnMoreButtonId);
there are some more TextView and EditText that dont show up here but you get the point.Some more methods that do the calculations ,getters and setters.
This method
public void switchActivity(){
final Intent intentObj = new Intent(this,Information_activity.class);
startActivity(intentObj);
}
But I am not using onResume() , onPause() or any methods from the lifecycle apart from onCreate().
From some search that I have done I found out that I am doing something wrong with how I manage the activity lifecycle but I still can't find the solution.The dev documents didn't help me that much and a post with kinda the same problem as mine is old.
My question is, how the navigation from InformationActivity to CalculatorActivity should be done, so the functionality doesn't break when CalculatorActivity comes back to interact with the user.Which method should be called onResume()? , onRestart()? and how should it look like?
Thanks anyone who is willing to help.
P.S: As I mentioned , I have read the documents for the lifecycle of an Activity but I haven't found the solution.
instead of starting new activity everytime, finish the informationactivity.
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
You are creating too much activities moving going back and forth this way. You can use either destroy the activity with finish(); or you can also go back to previous activity using onBackPressed();
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
Try this out
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InformationActivity.this.finish();
}
});
}
Instead of saying where to go back, you can just finish the activity and it will automatically switch you to the previous one.
I think your activities are hierarchical thus you should be able to do the following from your main calculator activity:
Intent i = new Intent(this, InformationActivity.class);
startActivityForResult(i);
Your back button add this code:
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setResult(Result.OK);
finish();
}
});
You are all suggesting the same thing.Adding
Information_Activity.this.finish()
fixed the broken functionality , though you are all correct I can pick only one answer.
Thanks

Do I need to include a default Intent in order to return to the previous Activity?

I am working on a To Do List Android application (it happens to be for a class assignment, but that's not what I'm asking about--I've tried to leave out as much code as I could). The main screen displays a list of ToDo items with a button at the bottom to open the Add New ToDo Item screen.
On the Add New ToDo Item screen, there is a Cancel button.
Relevant ToDoManagerActivity.java snippet:
public void onCreate(Bundle savedInstanceState) {
// Init and setup adapter, etc.
footerView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ToDoManagerActivity.this, AddToDoActivity.class);
startActivityForResult(intent, ADD_TODO_ITEM_REQUEST);
}
});
// Attach the adapter to this ListActivity's ListView
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
log("Entered onActivityResult()");
// Check result code and request code.
// If user submitted a new ToDoItem
// Create a new ToDoItem from the data Intent
// and then add it to the adapter
}
Relevant AddToDoActivity.java snippet:
protected void onCreate(Bundle savedInstanceState) {
// Initialize default view, handle other events, etc.
final Button cancelButton = (Button) findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setResult(RESULT_CANCELED, new Intent());
finish();
}
});
}
The above code works. Previously, I was trying this in the onClick handler for cancelButton:
public void onClick(View v) {
finishActivity(RESULT_CANCELED);
}
When I clicked the Cancel button, I could see that the onActivityResult was being reached in the logs, but the screen was not reverting back to the main ToDo list screen.
Why does the above code not return me to the previous screen, but the following code does return me to the previous screen? What am I misunderstanding about the task backstack/activities?
public void onClick(View v) {
setResult(RESULT_CANCELED, new Intent());
finish();
}
According to the documentation:
public void finish ()
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via
onActivityResult().
and
public void finishActivity (int requestCode)
Force finish another activity that you had previously started with startActivityForResult(Intent, int).
You should call finish() to close the current activity and finishActivity() to close another activity you started using startActivityForResult(Intent intent, int requestCode). Calling finishActivity() on the current activity will not close it.
Also, there's no point in creating a new Intent for setResult() as you are not passing back any data. Doing this would be sufficient:
setResult(RESULT_CANCELED);
finish();
From Android Docs:
public void finishActivity (int requestCode)
Force finish another activity that you had previously started with startActivityForResult(Intent, int).
finishActivity does not finish the current activity but calls finish for an activity called with requestCode
If you look at the documentation for finishActivity() it says that it will force finish an activity started with startActivityForResult(), but you have to pass in the request code that you used to start the other activity. In your case it would be ADD_TODO_ITEM_REQUEST.
This is probably not the API you want to use. Your 2nd method is cleaner in that you don't need to force close the child activity, but let it finish in the normal way.

How to Restart my Previous Activity from Next Activity

I have critical stage can anyone help if you know,Actually i have 1 to 5 activities and 6th one is common activity,first i need to go 1st activity to 6th activity,Now my doubt is in 6th activity having one refresh button for activity 1st activity but its not working in my code,i have used
_Try_Again.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*Intent loginIntent = new Intent(Inter_Conn_Error_activity.this,
Splashscreen.class);
startActivity(loginIntent);*/
onBackPressed();
finish();
}
});...
How can i do this? please help me..
I cant get the problem u r facing...
But as i understand it then:
1 to 6
intent (this,6)
6 to 1
intent or finish()
6 to 6
intent or call onCreate method
for this use the attribute,
Putextra
when u try to go common activity take the avivity name with it.
when u try to come back to previous activity
check which activity it came...
You could override the onActivityResult callback from the calling activity. Once the new activity finishes, the onActivityResult kicks in and you could use it.
Eg:
#Override
public void onActivityResult(int,int, Intent)
{
// YOUR CODE
}

Java/Android How to call an activity into a method?

How do i call on a seperate activity within a method:
For example:
private void startApp() {
Patient_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// I want this Button to go to an Detailed_ModeActivity
// This is how i Am doing it right now, but it comes out with an
// error
Intent b = new Intent(this, Detailed_ModeActivity.class);
startActivity(b);
}
});
}
Any Help would be appreciated.
The Button was declared in the onCreate method
Like this:
Intent b = new Intent(v.getContext(), Detailed_ModeActivity.class);
startActivity(b);
The this refers to an View.OnClickListener object which doesn't have startActivity() method and cannot be passed to an Intent. You need to call startActivity() on a Context (e.g. an Activity). Let's say your code is in the MainActivity class. Like this:
Intent b = new Intent(MainActivity.this, Detailed_ModeActivity.class);
MainActivity.this.startActivity(b);
First up, make sure Detailed_ModeActivity extends Activity.
Secondly you need to add the activity class to the manifest.xml file if you haven't already.

Categories