How I can refresh activity on start Kotlin? - java

How can I refresh or update activity when it is starting?
I have code ThemeSwitcher in SecondActivity, when I switch theme and return to MainActivity, theme is't changes, only when I restart an app

You should open your MainActivity then finish your second activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
Then you should set your theme before super.onCreate(savedInstanceState);
#Override
protected void onCreate(Bundle savedInstanceState) {
setYourTheme();
super.onCreate(savedInstanceState);
}

Related

How to save last visited activity and launch it on start

I have the MainActivity which is welcome activity and some other ones.
Lets suppose Act1, Act2 and Act3.
I want the program automatically redirect from MainActivity directly to last visited activity on every app launch.
whether Act1, Act2 or Act3
Well, I tried to do this jump from main activity to last visited activity, but it didn't work.
here's code what I've already tried and didn't work:
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadLocale();
setContentView(R.layout.activity_main);
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
int restoredLevel = prefs.getInt("level", 0);
if (restoredLevel > 0) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
Act1:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Act1);
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putInt("level", 1);
editor.apply();
}
(I simply want to change launcher activity depending on last visited activity, if you know any better solution for my problem, I will be thankful)

Pressing the back button goes back to first launched activity

I have an activity which starts when my app is first launched(Only once).The activity lets the users select the topics.Then when they press done, I finish() the activity.This leads the user to the MainActivity.But when the user press the back button it goes back to the activity which was first launched(the activity which I closed using finish()).But what i want is that the app should close when the user presses the back button from the MainActivity(always).I overrided onBackPressed in both the classes.
My onBackPressed method in both the classes looks like this:
#Override
public void onBackPressed() {
finish();
}
MainActivity(Relevant part of code):
public class MainActivity extends AppCompatActivity
{
TopicAdapter adapter;
private AdView mAdView ;
//-------GLOBAL VARIABLES-------------------
AdRequest adRequest;
ArrayList<Sections> gameList = new ArrayList<>();
String json;
//-------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPreferences =
getSharedPreferences("ShaPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
boolean firstTime=sharedPreferences.getBoolean("first", true);
//Launch the topics selection activity for the first time
if(firstTime)
{
editor.putBoolean("first",false);
editor.commit();
Intent intent = new Intent(MainActivity.this, channels_activity.class);
intent.putExtra("isFirst",true);
startActivity(intent);
}
}
#Override
public void onBackPressed()
{
finish();
}
}
You just need to call finish()
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
And Set android:noHistory="true" on the activity in your manifest
See here

Removing Activity from BackStack (Flag_Activity_no_history) does not work

I've a Start button in my MainActivity. If I click on this button I go to the next Activity (InfoActivity). Now, I want to remove the MainActivity from the BackStack, if I click on the button. I've tried this:
View.OnClickListener startButtonListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
};
But that removes not the MainActivity from the BackStack, it removes the InfoActivity from the BackStack.
I know that I could insert the flag into the AndroidManifest. But that is not possible for me, because if I go to the PreferencesActivity from the MainActivity and I using the flag in the AndroidManifest, the MainActivity is removed if I return from the PreferencesActivity back to the MainActivity.
So I want to remove the MainActivity from the BackStack, if I click on the button Start.
Add finish();
Like this :
View.OnClickListener startButtonListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
this.finish();
}
};
As said you should use Intent.FLAG_ACTIVITY_NO_HISTORY on the activity you dont want to track in your case on MainActivity.
Other way to do it is Intent.FLAG_ACTIVITY_CLEAR_TOP on your Info activity this will clear whole history (even things before MainActivity)

intent does not launch any activity

In my %MainActivity I have this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this,
LoginSignupActivity.class);
startActivity(intent);
....
}
In fact, the intent is wrapped by a some authentication check but to debug I deleted it, but this method doesn't launch any LoginSignupActivity. Could someone help ?
Edit:
Here's the entire onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this,
LoginSignupActivity.class);
startActivity(intent);
setActionBarListNavigation();
setupDrawer();
setupContainer();
}
MainActivity extends CustomActivity (which lods data from AsyncLoader) and this last Activity inherits from FragmentActivity and has an onCreate method
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
theme = getAppTheme();
setupActionBar();
}
with
protected void setupActionBar()
{
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setBackgroundDrawable(getResources().getDrawable(theme));
}
At last
add: startActivity(intent);
Intent intent = new Intent(this, LoginSignupActivity.class);
startActivity(intent);
and add LoginSignupActivity into your Manifest.xml
<activity
android:name=".LoginSignupActivity"
android:label="Login" >
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this,
LoginSignupActivity.class);
startActivity(intent);
}
register your all activity in manifest.xml
<activity android:name=".LoginSignupActivity"/>
Just had this problem, I needed to add my custom jar library to the build.gradle file:
dependencies {
compile files('libs/android-support-v4.jar')
ADD FILE HERE
}
/**
* A {#code TaskSelectionException} is thrown when the tasks to execute cannot be selected due to some user input
* problem.
*/
public class TaskSelectionException extends InvalidUserDataException {
public TaskSelectionException(String message) {
super(message);
}
}
You are launching an Activity during "onCreate" - you will not see your main activity (it never reaches "onStart" or "onResume").
So, if you are having problems, it is not in your MainActivity, it is in the other one, LoginSignupActivity.

Using Intent in Android

I am currently learning Android app development and I am a bit confused on how to use Intent. I am trying to make a "To Do list" app. My problem right now is that I want to be able to tap the item in my to do list to go to a Edit Item page.
Here is what I have so far.
ToDoActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
setupListViewListener();
setupEditItemListener();
}
The activity I want to launch is called EditItemListener. These are the two functions that I am playing Intent with. Right now I am just testing how to display the EditItemActivity.
private void launchEditItem() {
Intent i = new Intent(this, EditItemActivity);
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
launchEditItem();
}
});
}
You should specify it as a class
Intent i = new Intent(this, EditItemActivity.class);
Also if you want to kill the current activity
use finish() after StartActivity()
on a listview set the OnItemClickListener and on the listener do this
Intent i = new Intent(this, YourActivity.class); //where you are (this) and where you go (YourAcitivity.class)
startActivity(i); //Now GO!
The intent will start another activity, this activity must be declared on AndroidMainfest.xml

Categories